﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Stone.Components.Entities;
using Stone.Services.EntityServices;
using Dare.Utilities;
using System.Xml;
using System.IO;
using System.Threading;
using System.Text;

namespace Stone.Web.pages
{
    public partial class MainPage : System.Web.UI.Page
    {
        PatientService patientService = Stone.Services.Application.Instance.GetDbService<PatientService>();
        HistoryService historyService = Stone.Services.Application.Instance.GetDbService<HistoryService>();
        HistoryFamilyService historyFamilyService = Stone.Services.Application.Instance.GetDbService<HistoryFamilyService>();
        HistoryEndocrineService historyEndocrineService = Stone.Services.Application.Instance.GetDbService<HistoryEndocrineService>();
        HospitalizationService hospitalizationService = Stone.Services.Application.Instance.GetDbService<HospitalizationService>();
        ChemistryService chemService = Stone.Services.Application.Instance.GetDbService<ChemistryService>();
        ChemistryUrine24hService chem24hurineService = Stone.Services.Application.Instance.GetDbService<ChemistryUrine24hService>();
        ChemistryBiochemistryService chemBiochemistryService = Stone.Services.Application.Instance.GetDbService<ChemistryBiochemistryService>();
        ChemistryBloodService chemBloodService = Stone.Services.Application.Instance.GetDbService<ChemistryBloodService>();
        ChemistryCruorService chemCruorService = Stone.Services.Application.Instance.GetDbService<ChemistryCruorService>();
        ChemistryImmunityService chemImmunityService = Stone.Services.Application.Instance.GetDbService<ChemistryImmunityService>();
        ChemistryLymphocyteService chemLymphocyteService = Stone.Services.Application.Instance.GetDbService<ChemistryLymphocyteService>();
        ChemistryUrineService chemUrineService = Stone.Services.Application.Instance.GetDbService<ChemistryUrineService>();
        ChemistryPGService chemPGService = Stone.Services.Application.Instance.GetDbService<ChemistryPGService>();
        ChemistrySpermService chemSpermService = Stone.Services.Application.Instance.GetDbService<ChemistrySpermService>();
        OpticsService opticsService = Stone.Services.Application.Instance.GetDbService<OpticsService>();
        SurgeryService surgeryService = Stone.Services.Application.Instance.GetDbService<SurgeryService>();
        SurgeryHardtubeService surgeryHardtubeService = Stone.Services.Application.Instance.GetDbService<SurgeryHardtubeService>();
        SurgeryKidneyService surgeryKidneyService = Stone.Services.Application.Instance.GetDbService<SurgeryKidneyService>();
        SurgerySofttubeService surgerySofttubeService = Stone.Services.Application.Instance.GetDbService<SurgerySofttubeService>();
        SurgeryPGService surgeryPGService = Stone.Services.Application.Instance.GetDbService<SurgeryPGService>();
        SurgerySpermService surgerySpermService = Stone.Services.Application.Instance.GetDbService<SurgerySpermService>();
        SurgeryInfectionService surgeryInfectionService = Stone.Services.Application.Instance.GetDbService<SurgeryInfectionService>();
        MediaFileService mediaFileService = Stone.Services.Application.Instance.GetDbService<MediaFileService>();


        protected void Page_Load(object sender, EventArgs e)
        {
        }

        [DirectMethod]
        public void DoLogin(string name, string password)
        {
            if (name == "admin" && password == "admin")
            {
                this.WindowLogin.Hide();
            }
            else
            {
                this.lblInfo.Text = "用户名或密码错误！";
                this.lblInfo.Show();
            }
        }

        [DirectMethod]
        public void AddPatient()
        {
            ClearPatientInfoWin();

            this.PatientTabPanel.ActiveTabIndex = 0;
            this.patientId.Text = "0";
            this.WindowPatientInfo.Show();
        }

        private void ClearPatientInfoWin()
        {
            //清空基本信息
            this.patientId.Reset();
            this.TextFieldName.Reset();
            this.DateFieldBirth.Reset();
            this.NumberFieldHeight.Reset();
            this.NumberFieldBMI.Reset();
            this.TextFieldAddress.Reset();
            this.NumberFieldTelephone.Reset();
            this.ComboBoxSex.Reset();
            this.TextFieldNameAbbr.Reset();
            this.NumberFieldAge.Reset();
            this.NumberFieldWeight.Reset();
            this.NumberFieldNum.Reset();
            this.NumberFieldPost.Reset();
            this.NumberFieldCellphone.Reset();
            this.TextAreaComment.Reset();
            this.FieldSetMenses.Hide();
            this.chkboxHasMenses.Checked = false;
            this.NumberFieldMensesCycle.Reset();
            this.NumberFieldMensesDay.Reset();

            //清空既往史信息
            this.CheckboxHistoryFamilyKidney.Checked = false;
            this.CheckboxHistoryFamilyUrine.Checked = false;
            this.CheckboxHistoryFamilyBlood.Checked = false;
            this.TextAreaHistoryFamily.Text = "";
            this.FieldSetHistoryFamily.Collapse();
            this.CheckboxHistoryEndocrinePrimary.Checked = false;
            this.CheckboxHistoryEndocrineUrine.Checked = false;
            this.CheckboxHistoryEndocrineKidney.Checked = false;
            this.CheckboxHistoryEndocrineVitaminD.Checked = false;
            this.TextAreaHistoryEndocrine.Text = "";
            this.FieldSetHistoryEndocrine.Collapse();
            this.CheckboxHistorySurgery.Checked = false;
            this.CheckboxHistoryPaishi.Checked = false;
            this.TextAreaHistoryMedicine.Text = "";

            //清空就医信息数据
            this.StoreHospital.RemoveAll();

        }

        [DirectMethod]
        public bool DeletePatient(string patientIdsStr)
        {
            Patient entity = null;
            try
            {
                if (patientIdsStr.IndexOf(',') == -1)
                {
                    int patientId;

                    if (int.TryParse(patientIdsStr, out patientId))
                    {
                        entity = patientService.Get(patientId);

                        if (patientService.DeletePatient(entity))
                        {
                            //LogOperation(UserLogOperationType.Delete, entity);
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    return false;
                }
                else
                {
                    string[] patientIds = patientIdsStr.Split(new char[] { ',' });

                    foreach (string patientIdStr in patientIds)
                    {
                        int patientId;

                        if (int.TryParse(patientIdStr, out patientId))
                        {
                            entity = patientService.Get(patientId);

                            if (patientService.DeletePatient(entity))
                            {
                                //LogOperation(UserLogOperationType.Delete, entity);
                            }
                            else
                            {
                                return false;
                            }
                        }
                    }
                    return true;
                }
            }
            catch (Exception ex)
            {
                //if (entity != null)
                //    LogOperationException(UserLogOperationType.Delete, entity, ex);
                return false;
            }
        }

        [DirectMethod]
        public bool GetPatientInfo(long id)
        {
            try
            {
                ClearPatientInfoWin();

                Patient patient = patientService.Get(id);

                this.patientId.Text = id.ToString();
                this.TextFieldName.Text = patient.Name;
                this.ComboBoxSex.SetValue(patient.Sex.ToString());
                //this.ComboBoxSex.AddScript("ComboBoxSexSelect(null,null,index);");
                if (patient.Birthday != null) this.DateFieldBirth.SetValue(patient.Birthday.Value.ToShortDateString());

                if (patient.Height != null) this.NumberFieldHeight.Text = patient.Height.ToString();
                if (patient.Bmi != null) this.NumberFieldBMI.Text = patient.Bmi.ToString();
                this.TextFieldAddress.Text = patient.Address;
                if (!String.IsNullOrEmpty(patient.Telephone)) this.NumberFieldTelephone.Text = patient.Telephone.ToString();

                this.TextFieldNameAbbr.Text = patient.NameAbbr;
                if (patient.Age != null) this.NumberFieldAge.Text = patient.Age.ToString();
                if (patient.Weight != null) this.NumberFieldWeight.Text = patient.Weight.ToString();
                if (patient.Number != null) this.NumberFieldNum.Text = patient.Number.ToString();
                if (!String.IsNullOrEmpty(patient.Postcode)) this.NumberFieldPost.Text = patient.Postcode.ToString();
                if (!String.IsNullOrEmpty(patient.Cellphone)) this.NumberFieldCellphone.Text = patient.Cellphone.ToString();

                this.TextAreaComment.Text = patient.Comment;

                if (patient.Sex == SexEnum.female)
                {
                    this.FieldSetMenses.Show();
                    if (patient.HasMenses.Value) chkboxHasMenses.Checked = true;
                    if(patient.MensesCycle.HasValue)this.NumberFieldMensesCycle.Text = patient.MensesCycle.Value.ToString();
                    if (patient.AmenorrheaTime.HasValue) this.NumberFieldMensesDay.Text = patient.AmenorrheaTime.Value.ToString();
                }

                if (patient.HistoryId.HasValue)
                {
                    Stone.Components.Entities.History history = historyService.Get(patient.HistoryId.Value);
                    if (history.StoneSurgeryHistory.Value) this.CheckboxHistorySurgery.Checked = true;
                    if (history.PaishiHistory.Value) this.CheckboxHistoryPaishi.Checked = true;
                    this.TextAreaHistoryMedicine.Text = history.MedicineHistory;

                    if (history.HistoryFamilyId.HasValue)
                    {
                        this.FieldSetHistoryFamily.Expand();
                        HistoryFamily historyFamily = historyFamilyService.Get(history.HistoryFamilyId.Value);
                        if (historyFamily.KidneyStone.Value) this.CheckboxHistoryFamilyKidney.Checked = true;
                        if (historyFamily.Hypercalcinuria.Value) this.CheckboxHistoryFamilyUrine.Checked = true;
                        if (historyFamily.Hypercalcinemia.Value) this.CheckboxHistoryFamilyBlood.Checked = true;
                        this.TextAreaHistoryFamily.Text = historyFamily.Comment;
                    }

                    if (history.HistoryEndocrineId.HasValue)
                    {
                        this.FieldSetHistoryEndocrine.Expand();
                        HistoryEndocrine historyEndocrine = historyEndocrineService.Get(history.HistoryEndocrineId.Value);
                        if (historyEndocrine.PrimaryHyperparathyroidism.Value) this.CheckboxHistoryEndocrinePrimary.Checked = true;
                        if (historyEndocrine.Hypercalcinuria.Value) this.CheckboxHistoryEndocrineUrine.Checked = true;
                        if (historyEndocrine.RenalTubularAcidosis.Value) this.CheckboxHistoryEndocrineKidney.Checked = true;
                        if (historyEndocrine.VitaminDIntoxication.Value) this.CheckboxHistoryEndocrineVitaminD.Checked = true;
                        this.TextAreaHistoryEndocrine.Text = historyEndocrine.Comment;
                    }
                }

                this.PagingToolbar2.SetPageIndex(0);
                //this.StoreHospital_RefreshData(null,new StoreRefreshDataEventArgs());

                this.PatientTabPanel.ActiveTabIndex = 0;
                this.WindowPatientInfo.Show();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        [DirectMethod]
        public bool SavePatient()
        {
            try
            {
                long patientId;
                if (!long.TryParse(this.patientId.Text, out patientId)) patientId = 0;

                if (String.IsNullOrEmpty(this.TextFieldName.Text))
                {
                    X.Msg.Alert("错误", "病人姓名不能为空！").Show();
                    return false;
                }

                SavePatient_Impl(patientId);

                this.WindowPatientInfo.Hide();

                X.Msg.Alert("成功", "保存病人信息成功！").Show();
                return true;
            }
            catch
            {
                X.Msg.Alert("错误", "保存病人信息失败！").Show();
                return false;
            }
        }

        private void SavePatient_Impl(long patientId)
        {
            Patient patient;

            if (patientId == 0)
            {
                patient = new Patient();
            }
            else
            {
                patient = patientService.Get(patientId);
            }

            #region 保存基本信息
            patient.Name = this.TextFieldName.Text;

            //patient.Birthday = this.DateFieldBirth.SelectedDate;
            DateTime dt = new DateTime(1, 1, 1);
            if (this.DateFieldBirth.SelectedDate != dt)
            {
                patient.Birthday = this.DateFieldBirth.SelectedDate;
            }
            else
            {
                patient.Birthday = null;
            }

            if (!String.IsNullOrEmpty(this.NumberFieldHeight.Text))
                patient.Height = decimal.Parse(this.NumberFieldHeight.Value.ToString());
            else
                patient.Height = null;

            if (!String.IsNullOrEmpty(this.NumberFieldBMI.Text))
                patient.Bmi = decimal.Parse(this.NumberFieldBMI.Value.ToString());
            else
                patient.Bmi = null;

            patient.Address = this.TextFieldAddress.Text;
            patient.Telephone = this.NumberFieldTelephone.Text;

            patient.NameAbbr = this.TextFieldNameAbbr.Text;

            if (!String.IsNullOrEmpty(this.NumberFieldAge.Text))
                patient.Age = int.Parse(this.NumberFieldAge.Value.ToString());
            else
                patient.Age = null;

            if (!String.IsNullOrEmpty(this.NumberFieldWeight.Text))
                patient.Weight = decimal.Parse(this.NumberFieldWeight.Value.ToString());
            else
                patient.Weight = null;

            if (!String.IsNullOrEmpty(this.NumberFieldNum.Text))
                patient.Number = int.Parse(this.NumberFieldNum.Value.ToString());
            else
                patient.Number = null;

            patient.Postcode = this.NumberFieldPost.Text;
            patient.Cellphone = this.NumberFieldCellphone.Text;

            patient.Comment = this.TextAreaComment.Text;

            patient.Sex = (SexEnum)Enum.Parse(typeof(SexEnum), this.ComboBoxSex.SelectedItem.Value);

            if (patient.Sex == SexEnum.female)
            {
                if (chkboxHasMenses.Checked)
                    patient.HasMenses = true;
                else
                    patient.HasMenses = false;

                //patient.MensesCycle = int.Parse(this.NumberFieldMensesCycle.Value.ToString());
                if (!String.IsNullOrEmpty(this.NumberFieldMensesCycle.Text))
                    patient.MensesCycle = int.Parse(this.NumberFieldMensesCycle.Value.ToString());
                else
                    patient.MensesCycle = null;

                //patient.AmenorrheaTime = int.Parse(this.NumberFieldMensesDay.Value.ToString());
                if (!String.IsNullOrEmpty(this.NumberFieldMensesDay.Text))
                    patient.AmenorrheaTime = int.Parse(this.NumberFieldMensesDay.Value.ToString());
                else
                    patient.AmenorrheaTime = null;

            }

            #endregion

            #region 保存既往史信息

            Stone.Components.Entities.History history;

            if (patient.HistoryId.HasValue)
            {
                history = historyService.Get(patient.HistoryId.Value);
            }
            else
            {
                history = new Stone.Components.Entities.History();
            }

            HistoryFamily historyFamily;
            if (FieldSetHistoryFamily.Collapsed)
            {
                if (history.HistoryFamilyId.HasValue)
                {
                    if (historyFamilyService.Delete(history.HistoryFamilyId.Value))
                        history.HistoryFamilyId = null;
                }
            }
            else
            {
                if (history.HistoryFamilyId.HasValue)
                {
                    historyFamily = historyFamilyService.Get(history.HistoryFamilyId.Value);
                }
                else
                {
                    historyFamily = new HistoryFamily();
                }

                if (this.CheckboxHistoryFamilyKidney.Checked)
                    historyFamily.KidneyStone = true;
                else
                    historyFamily.KidneyStone = false;

                if (this.CheckboxHistoryFamilyUrine.Checked)
                    historyFamily.Hypercalcinuria = true;
                else
                    historyFamily.Hypercalcinuria = false;

                if (this.CheckboxHistoryFamilyBlood.Checked)
                    historyFamily.Hypercalcinemia = true;
                else
                    historyFamily.Hypercalcinemia = false;

                historyFamily.Comment = this.TextAreaHistoryFamily.Text;

                if (historyFamily.Id == 0)
                {
                    historyFamilyService.Insert(historyFamily);
                    history.HistoryFamilyId = historyFamily.Id;
                }
                else
                {
                    historyFamilyService.Update(historyFamily);
                }
            }

            HistoryEndocrine historyEndocrine;
            if (FieldSetHistoryEndocrine.Collapsed)
            {
                if (history.HistoryEndocrineId.HasValue)
                {
                    if (historyEndocrineService.Delete(history.HistoryEndocrineId.Value))
                        history.HistoryEndocrineId = null;
                }
            }
            else
            {
                if (history.HistoryEndocrineId.HasValue)
                {
                    historyEndocrine = historyEndocrineService.Get(history.HistoryEndocrineId.Value);
                }
                else
                {
                    historyEndocrine = new HistoryEndocrine();
                }

                if (this.CheckboxHistoryEndocrinePrimary.Checked)
                    historyEndocrine.PrimaryHyperparathyroidism = true;
                else
                    historyEndocrine.PrimaryHyperparathyroidism = false;

                if (this.CheckboxHistoryEndocrineUrine.Checked)
                    historyEndocrine.Hypercalcinuria = true;
                else
                    historyEndocrine.Hypercalcinuria = false;

                if (this.CheckboxHistoryEndocrineKidney.Checked)
                    historyEndocrine.RenalTubularAcidosis = true;
                else
                    historyEndocrine.RenalTubularAcidosis = false;

                if (this.CheckboxHistoryEndocrineVitaminD.Checked)
                    historyEndocrine.VitaminDIntoxication = true;
                else
                    historyEndocrine.VitaminDIntoxication = false;

                historyEndocrine.Comment = this.TextAreaHistoryEndocrine.Text;

                if (historyEndocrine.Id == 0)
                {
                    historyEndocrineService.Insert(historyEndocrine);
                    history.HistoryEndocrineId = historyEndocrine.Id;
                }
                else
                {
                    historyEndocrineService.Update(historyEndocrine);
                }
            }

            if (this.CheckboxHistorySurgery.Checked)
                history.StoneSurgeryHistory = true;
            else
                history.StoneSurgeryHistory = false;

            if (this.CheckboxHistoryPaishi.Checked)
                history.PaishiHistory = true;
            else
                history.PaishiHistory = false;

            history.MedicineHistory = this.TextAreaHistoryMedicine.Text;

            if (history.Id == 0)
            {
                historyService.Insert(history);
                patient.HistoryId = history.Id;
            }
            else
            {
                historyService.Update(history);
            }

            #endregion



            if (patientId == 0)
            {
                patientService.Insert(patient);
            }
            else
            {
                patientService.Update(patient);
            }

            history.PatientId = patient.Id;
            historyService.Update(history);

            this.patientId.Text = patient.Id.ToString();
        }

        protected void StorePatientInfo_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            string sortDir = e.Dir == Ext.Net.SortDirection.Default ? "" : " " + e.Dir.ToString();
            string orderBy = "id" + sortDir;
            switch (e.Sort)
            {
                case "name":
                    orderBy = "name" + sortDir;
                    break;
                case "sex":
                    orderBy = "sex" + sortDir;
                    break;
                case "birth":
                    orderBy = "birthday" + sortDir;
                    break;
                case "age":
                    orderBy = "age" + sortDir;
                    break;
                case "no":
                    orderBy = "number" + sortDir;
                    break;
                case "height":
                    orderBy = "height" + sortDir;
                    break;
                case "weight":
                    orderBy = "weight" + sortDir;
                    break;
                case "bmi":
                    orderBy = "BMI" + sortDir;
                    break;
                case "address":
                    orderBy = "address" + sortDir;
                    break;
                case "postcode":
                    orderBy = "postcode" + sortDir;
                    break;
                case "telephone":
                    orderBy = "telephone" + sortDir;
                    break;
                case "cellphone":
                    orderBy = "cellphone" + sortDir;
                    break;
                case "comment":
                    orderBy = "comment" + sortDir;
                    break;
            }

            string querySql = null;
            string searchPatientName = null;

            if (!String.IsNullOrEmpty(this.hiddenQuerySql.Text))
            {
                querySql = this.hiddenQuerySql.Text;
            }
            else
            {
                if (!string.IsNullOrEmpty(e.Parameters["searchText"]))
                {
                    searchPatientName = e.Parameters["searchText"].ToString();
                }
            }

            int totalCount = 0;
            List<Patient> list = null;

            list = patientService.SearchPatientInfo(querySql,searchPatientName, orderBy, e.Start / e.Limit, e.Limit, out totalCount);

            if (list != null)
            {
                this.StorePatientInfo.DataSource = Array.ConvertAll<Patient, object[]>(list.ToArray(),
                    delegate(Patient item)
                    {
                        return new object[]
                        {
                            item.Id,
                            item.Name,
                            EnumDisplayNameAttribute.GetDisplayName(item.Sex),
                            item.Birthday == null ? "" : item.Birthday.Value.ToString("yyyy-MM-dd"),
                            item.Age == null?"":item.Age.Value.ToString(),
                            item.Number == null?"":item.Number.Value.ToString(),
                            item.Height == null?"":item.Height.Value.ToString(),
                            item.Weight == null?"":item.Weight.Value.ToString(),
                            item.Bmi == null?"":item.Bmi.Value.ToString(),
                            item.Address,
                            item.Postcode,
                            item.Telephone,
                            item.Cellphone,
                            item.Comment
                        };
                    });
                this.StorePatientInfo.DataBind();
            }
            else
            {
                this.StorePatientInfo.RemoveAll();
            }

            //e.TotalCount = totalCount;
            (this.StorePatientInfo.Proxy[0] as PageProxy).Total = totalCount;
        }



        #region 就医信息

        [DirectMethod]
        public void AddHospitalInfo()
        {
            bool newPatient = false;
            try
            {
                if (this.patientId.Text == "0")
                {
                    if (String.IsNullOrEmpty(this.TextFieldName.Text))
                    {
                        X.Msg.Alert("错误", "病人姓名不能为空！").Show();
                        return;
                    }

                    SavePatient_Impl(0);

                    newPatient = true;
                    //X.Msg.Alert("成功", "保存病人信息成功！").Show();
                }
            }
            catch
            {
                X.Msg.Alert("错误", "保存病人信息失败！").Show();
                return;
            }

            ClearWindowHospitalInfo();

            this.HiddenHospitalId.Text = "0";

            this.TabPanelNewHospitalInfo.ActiveTabIndex = 0;
            this.WindowHospitalInfo.Title = "添加就医信息";
            this.WindowHospitalInfo.Show();

            if (newPatient) X.Msg.Alert("成功", "保存病人信息成功！").Show();
        }

        private void ClearWindowHospitalInfo()
        {
            //清空化学检查
            this.DateFieldHospitalInfo.Reset();
            this.DateFieldHospitalInfo.Enabled = true;
            this.HiddenHospitalId.Text = "";

            this.ChemistryBloodWBC.Reset();
            this.ChemistryBloodLYMl.Reset();
            this.ChemistryBloodM0l.Reset();
            this.ChemistryBloodGRANl.Reset();
            this.ChemistryBloodE0l.Reset();
            this.ChemistryBloodBAS0l.Reset();
            this.ChemistryBloodLYMp.Reset();
            this.ChemistryBloodM0p.Reset();
            this.ChemistryBloodE0p.Reset();
            this.ChemistryBloodBAS0p.Reset();
            this.ChemistryBloodGRANp.Reset();
            this.ChemistryBloodRBC.Reset();
            this.ChemistryBloodHGB.Reset();
            this.ChemistryBloodHCT.Reset();
            this.ChemistryBloodPLT.Reset();
            this.FieldSetHospitalChemistryBlood.Collapse();

            this.ChemistryCruorPT.Reset();
            this.ChemistryCruorAPTT.Reset();
            this.ChemistryCruorTT.Reset();
            this.ChemistryCruorFIB.Reset();
            this.ChemistryCruorFDP.Reset();
            this.ChemistryCruorD2.Reset();
            this.FieldSetHospitalChemistryCruor.Collapse();

            this.ChemistryUrineBZ.Reset();
            this.ChemistryUrineSJD.Reset();
            this.ChemistryUrineWhite.Reset();
            this.ChemistryUrineYXSY.Reset();
            this.ChemistryUrineDBZ.Reset();
            this.ChemistryUrineRCul.Reset();
            this.ChemistryUrineRChp.Reset();
            this.ChemistryUrineWCul.Reset();
            this.ChemistryUrineWChp.Reset();
            this.ChemistryUrineGCul.Reset();
            this.ChemistryUrineGChp.Reset();
            this.ChemistryUrineBLGX.Reset();
            this.ChemistryUrineJJ.Reset();
            this.FieldSetChemistryUrine.Collapse();

            this.ChemistryImmunityIL2.Reset();
            this.ChemistryImmunityIL6.Reset();
            this.ChemistryImmunityIL8.Reset();
            this.ChemistryImmunityIL10.Reset();
            this.ChemistryImmunityTNFa.Reset();
            this.ChemistryImmunityCRP.Reset();
            this.ChemistryImmunityNms.Reset();
            this.FieldSetHospitalChemistryMygn.Collapse();

            this.ChemistryLbxbCD3.Reset();
            this.ChemistryLbxbCD4.Reset();
            this.ChemistryLbxbCD8.Reset();
            this.ChemistryLbxbCD19.Reset();
            this.ChemistryLbxbCD1656.Reset();
            this.ChemistryLbxbTBNK.Reset();
            this.ChemistryLbxbHS.Reset();
            this.FieldSetHospitalChemistryLbxb.Collapse();

            this.ChemistryXshNS2.Reset();
            this.ChemistryXshNa.Reset();
            this.ChemistryXshKa.Reset();
            this.ChemistryXshCl.Reset();
            this.ChemistryXshCS.Reset();
            this.ChemistryXshCa.Reset();
            this.ChemistryXshPTT.Reset();
            this.ChemistryXshJG.Reset();
            this.ChemistryXshNS.Reset();
            this.ChemistryXshGYS.Reset();
            this.ChemistryXshL.Reset();
            this.ChemistryXshCO2.Reset();
            this.ChemistryXshBDB.Reset();
            this.FieldSetHospitalChemistryXsh.Collapse();

            this.Chemistry24hNSJD.Reset();
            this.Chemistry24hNCa.Reset();
            this.Chemistry24hNNa.Reset();
            this.Chemistry24hNKa.Reset();
            this.Chemistry24hNCl.Reset();
            this.Chemistry24hNCS.Reset();
            this.Chemistry24hNPTT.Reset();
            this.Chemistry24hNJG.Reset();
            this.Chemistry24hNL.Reset();
            this.Chemistry24hNNS.Reset();
            this.Chemistry24hNGYS.Reset();
            this.FieldSetHospitalChemistry24hN.Collapse();

            this.ChemistryPGLecithin.Reset();
            this.ChemistryPGRbc.Reset();
            this.ChemistryPGWbc.Reset();
            this.FieldSetHospitalChemistryPG.Collapse();

            this.ChemistrySpermCount.Reset();
            this.ChemistrySpermRate.Reset();
            this.ChemistrySpermPower.Reset();
            this.ChemistrySpermStatus.Reset();
            this.FieldSetHospitalChemistrySperm.Collapse();

            this.TextAreaHospitalChemistryZdnpyjym.Text = "";
            this.TextAreaHospitalChemistryJsxjpyjym.Text = "";
            this.TextAreaHospitalChemistryJscffx.Text = "";


            //清空光学诊断
            this.HiddenOpticsBFileId.Reset();
            this.ButtonOpticsBDelete.Disabled = true;
            this.ButtonOpticsBDownload.Disabled = true;
            this.HiddenOpticsCTFileId.Reset();
            this.ButtonOpticsCTDelete.Disabled = true;
            this.ButtonOpticsCTDownload.Disabled = true;
            this.HiddenOpticsCTUFileId.Reset();
            this.ButtonOpticsCTUDelete.Disabled = true;
            this.ButtonOpticsCTUDownload.Disabled = true;
            this.HiddenOpticsKUBIVPFileId.Reset();
            this.ButtonOpticsKUBIVPDelete.Disabled = true;
            this.ButtonOpticsKUBIVPDownload.Disabled = true;
            this.HiddenOpticsECTFileId.Reset();
            this.ButtonOpticsECTDelete.Disabled = true;
            this.ButtonOpticsECTDownload.Disabled = true;
            this.NumberFieldOpticsCT.Reset();
            this.NumberFieldOpticsAngle.Reset();
            this.NumberFieldOpticsGFR.Reset();
            this.TextAreaOpticsComment.Text = "";

            //清空手术信息
            this.DateFieldSurgery.Reset();
            //this.RadioGroupSurgeryStonePosition.Reset();
            this.RadioGroupSurgeryStonePosition.SetValue("RadioSurgeryStoneLeft", true);
            this.NumberFieldSurgeryStoneSize.Reset();
            this.HiddenSurgeryRecordFileId.Reset();
            this.ButtonSurgeryRecordDelete.Disabled = true;
            this.ButtonSurgeryRecordDownload.Disabled = true;
            this.TextAreaSurgeryStonePosition.Text = "";
            this.TextAreaSurgeryStoneRemain.Text = "";
            
            //this.RadioGroupSurgery.Reset();
            this.RadioGroupSurgery.SetValue("RadioSurgeryKidney", true);
            this.PanelSurgeryKidney.Enabled = true;
            this.PanelSurgerySoft.Enabled = false;
            this.PanelSurgeryHard.Enabled = false;
            this.PanelSurgeryPG.Enabled = false;
            this.PanelSurgerySperm.Enabled = false;
            this.TabPanelSurgery.ActiveTabIndex = 0;

            this.NumberFieldSurgeryKidneyChannel.Reset();
            this.ComboBoxSurgeryKidneyChannelSize.Reset();
            this.ComboBoxSurgeryKidneyEnter.Reset();
            this.HiddenSurgeryKidneyDSARecordFileId.Reset();
            this.ButtonSurgeryKidneyDSARecordDelete.Disabled = true;
            this.ButtonSurgeryKidneyDSARecordDownload.Disabled = true;
            this.HiddenSurgeryKidneyRecordFileId.Reset();
            this.ButtonSurgeryKidneyRecordDelete.Disabled = true;
            this.ButtonSurgeryKidneyRecordDownload.Disabled = true;
            this.CheckboxSurgeryKidneyBlood.Checked = false;
            //this.RadioGroupSurgeryKidneyTreat.Reset();
            this.RadioGroupSurgeryKidneyTreat.SetValue("RadioSurgeryKidneyTreatKeep", true);

            this.CheckboxSurgerySoftSlit.Checked = false;
            this.ComboBoxSurgerySoftExpansion.Reset();
            this.TextFieldSurgerySoftBasket.Reset();
            this.TextFieldSurgerySoftNarrow.Reset();
            //this.FieldSetSurgerySoftNarrow.Collapse();
            this.CheckboxSurgerySoftNarrow.Checked = false;
            this.TextFieldSurgerySoftNarrow.Hide();

            this.CheckboxSurgeryHardSlit.Checked = false;
            this.TextFieldSurgeryHardNarrow.Reset();
            //this.FieldSetSurgeryHardNarrow.Collapse();
            this.CheckboxSurgeryHardNarrow.Checked = false;
            this.TextFieldSurgeryHardNarrow.Hide();

            this.RadioGroupSurgeryPG.SetValue("点切", true);
            
            this.CheckboxSurgerySpermEms.Checked = false;

            this.CheckboxSurgeryInfectionFever.Checked = false;
            this.CheckboxSurgeryInfectionBlood.Checked = false;
            this.NumberFieldSurgeryInfectionType.Reset();
            this.NumberFieldSurgeryInfectionTime.Reset();
            this.CheckboxSurgeryInfectionCure.Checked = false;
            //this.FieldSetSurgeryInfection.Collapse();
            this.CheckboxSurgeryInfectionDisease.Checked = false;
            this.panelSurgeryInfectionDisease.Hide();
            this.CheckboxSurgeryInfection.Checked = false;
            this.panelSurgeryInfection.Hide();
            

            this.CheckboxChemistry.Checked = false;
            this.PanelHospitalChemistry.Disabled = true;
            this.CheckboxOptics.Checked = false;
            this.PanelHospitalOptics.Disabled = true;
            this.CheckboxSurgery.Checked = false;
            this.PanelHospitalSurgery.Disabled = true;

        }

        [DirectMethod]
        public bool DeleteHospitalInfo(string hospitalIdsStr)
        {
            Hospitalization entity = null;
            try
            {
                if (hospitalIdsStr.IndexOf(',') == -1)
                {
                    int hospitalId;

                    if (int.TryParse(hospitalIdsStr, out hospitalId))
                    {
                        entity = hospitalizationService.Get(hospitalId);

                        if (hospitalizationService.DeleteHospitalInfo(entity))
                        {
                            this.PagingToolbar2.SetPageIndex(0);
                            //StoreHospital_RefreshData(null, new StoreRefreshDataEventArgs());
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    return false;
                }
                else
                {
                    string[] hospitalIds = hospitalIdsStr.Split(new char[] { ',' });

                    foreach (string hospitalIdStr in hospitalIds)
                    {
                        int hospitalId;

                        if (int.TryParse(hospitalIdStr, out hospitalId))
                        {
                            entity = hospitalizationService.Get(hospitalId);

                            if (hospitalizationService.DeleteHospitalInfo(entity))
                            {
                                //LogOperation(UserLogOperationType.Delete, entity);
                            }
                            else
                            {
                                this.PagingToolbar2.SetPageIndex(0);
                                //StoreHospital_RefreshData(null, new StoreRefreshDataEventArgs());
                                return false;
                            }
                        }
                    }
                    this.PagingToolbar2.SetPageIndex(0);
                    //StoreHospital_RefreshData(null, new StoreRefreshDataEventArgs());
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        [DirectMethod]
        public bool GetHospitalInfo(long id)
        {
            try
            {
                ClearWindowHospitalInfo();

                Hospitalization hospitalInfo = hospitalizationService.Get(id);

                this.HiddenHospitalId.Text = id.ToString();

                this.DateFieldHospitalInfo.SetValue(hospitalInfo.Date.ToShortDateString());
                this.DateFieldHospitalInfo.Enabled = false;

                #region 化验检查
                if (hospitalInfo.ChemistryId.HasValue)
                {
                    this.CheckboxChemistry.Checked = true;
                    this.PanelHospitalChemistry.Disabled = false;

                    Chemistry chemistry = chemService.Get(hospitalInfo.ChemistryId.Value);

                    this.TextAreaHospitalChemistryZdnpyjym.Text = chemistry.UrineDrug;
                    this.TextAreaHospitalChemistryJsxjpyjym.Text = chemistry.StoneDrug;
                    this.TextAreaHospitalChemistryJscffx.Text = chemistry.Ingredient;

                    if (chemistry.ChemistryBloodId.HasValue)
                    {
                        this.FieldSetHospitalChemistryBlood.Expand();
                        ChemistryBlood chemBlood = chemBloodService.Get(chemistry.ChemistryBloodId.Value);
                        if (chemBlood.Wbc.HasValue)
                            this.ChemistryBloodWBC.Text = chemBlood.Wbc.ToString();
                        if (chemBlood.LymL.HasValue)
                            this.ChemistryBloodLYMl.Text = chemBlood.LymL.ToString();
                        if (chemBlood.M0L.HasValue)
                            this.ChemistryBloodM0l.Text = chemBlood.M0L.ToString();
                        if (chemBlood.GranL.HasValue)
                            this.ChemistryBloodGRANl.Text = chemBlood.GranL.ToString();
                        if (chemBlood.E0L.HasValue)
                            this.ChemistryBloodE0l.Text = chemBlood.E0L.ToString();
                        if (chemBlood.Bas0L.HasValue)
                            this.ChemistryBloodBAS0l.Text = chemBlood.Bas0L.ToString();
                        if (chemBlood.LymP.HasValue)
                            this.ChemistryBloodLYMp.Text = chemBlood.LymP.ToString();
                        if (chemBlood.M0P.HasValue)
                            this.ChemistryBloodM0p.Text = chemBlood.M0P.ToString();
                        if (chemBlood.E0P.HasValue)
                            this.ChemistryBloodE0p.Text = chemBlood.E0P.ToString();
                        if (chemBlood.Bas0P.HasValue)
                            this.ChemistryBloodBAS0p.Text = chemBlood.Bas0P.ToString();
                        if (chemBlood.GranP.HasValue)
                            this.ChemistryBloodGRANp.Text = chemBlood.GranP.ToString();
                        if (chemBlood.Rbc.HasValue)
                            this.ChemistryBloodRBC.Text = chemBlood.Rbc.ToString();
                        if (chemBlood.Hgb.HasValue)
                            this.ChemistryBloodHGB.Text = chemBlood.Hgb.ToString();
                        if (chemBlood.Hct.HasValue)
                            this.ChemistryBloodHCT.Text = chemBlood.Hct.ToString();
                        if (chemBlood.Plt.HasValue)
                            this.ChemistryBloodPLT.Text = chemBlood.Plt.ToString();
                    }
                    if (chemistry.ChemistryCruorId.HasValue)
                    {
                        this.FieldSetHospitalChemistryCruor.Expand();
                        ChemistryCruor chemCruor = chemCruorService.Get(chemistry.ChemistryCruorId.Value);
                        if (chemCruor.Pt.HasValue)
                            this.ChemistryCruorPT.Text = chemCruor.Pt.ToString();
                        if (chemCruor.Aptt.HasValue)
                            this.ChemistryCruorAPTT.Text = chemCruor.Aptt.ToString();
                        if (chemCruor.Tt.HasValue)
                            this.ChemistryCruorTT.Text = chemCruor.Tt.ToString();
                        if (chemCruor.Fib.HasValue)
                            this.ChemistryCruorFIB.Text = chemCruor.Fib.ToString();
                        if (chemCruor.Fdp.HasValue)
                            this.ChemistryCruorFDP.Text = chemCruor.Fdp.ToString();
                        if (chemCruor.Ddimer.HasValue)
                            this.ChemistryCruorD2.Text = chemCruor.Ddimer.ToString();
                    }
                    if (chemistry.ChemistryUrineId.HasValue)
                    {
                        this.FieldSetChemistryUrine.Expand();
                        ChemistryUrine chemUrine = chemUrineService.Get(chemistry.ChemistryUrineId.Value);
                        if (chemUrine.Weight.HasValue)
                            this.ChemistryUrineBZ.Text = chemUrine.Weight.ToString();
                        if (chemUrine.Ph.HasValue)
                            this.ChemistryUrineSJD.Text = chemUrine.Ph.ToString();
                        if (chemUrine.Wbc.HasValue)
                            this.ChemistryUrineWhite.Text = chemUrine.Wbc.ToString();
                        if (chemUrine.Nitrite.HasValue)
                            this.ChemistryUrineYXSY.Text = chemUrine.Nitrite.ToString();
                        if (chemUrine.Protein.HasValue)
                            this.ChemistryUrineDBZ.Text = chemUrine.Protein.ToString();
                        if (chemUrine.Rbcul.HasValue)
                            this.ChemistryUrineRCul.Text = chemUrine.Rbcul.ToString();
                        if (chemUrine.Rbchp.HasValue)
                            this.ChemistryUrineRChp.Text = chemUrine.Rbchp.ToString();
                        if (chemUrine.Wbcul.HasValue)
                            this.ChemistryUrineWCul.Text = chemUrine.Wbcul.ToString();
                        if (chemUrine.Wbchp.HasValue)
                            this.ChemistryUrineWChp.Text = chemUrine.Wbchp.ToString();
                        if (chemUrine.Tcul.HasValue)
                            this.ChemistryUrineGCul.Text = chemUrine.Tcul.ToString();
                        if (chemUrine.Tchp.HasValue)
                            this.ChemistryUrineGChp.Text = chemUrine.Tchp.ToString();
                        if (chemUrine.Tube.HasValue)
                            this.ChemistryUrineBLGX.Text = chemUrine.Tube.ToString();
                        if (chemUrine.Crystal.HasValue)
                            this.ChemistryUrineJJ.Text = chemUrine.Crystal.ToString();
                    }
                    if (chemistry.ChemistryImmunityId.HasValue)
                    {
                        this.FieldSetHospitalChemistryMygn.Expand();
                        ChemistryImmunity chemImmunity = chemImmunityService.Get(chemistry.ChemistryImmunityId.Value);
                        if (chemImmunity.Il2.HasValue)
                            this.ChemistryImmunityIL2.Text = chemImmunity.Il2.ToString();
                        if (chemImmunity.Il6.HasValue)
                            this.ChemistryImmunityIL6.Text = chemImmunity.Il6.ToString();
                        if (chemImmunity.Il8.HasValue)
                            this.ChemistryImmunityIL8.Text = chemImmunity.Il8.ToString();
                        if (chemImmunity.Il10.HasValue)
                            this.ChemistryImmunityIL10.Text = chemImmunity.Il10.ToString();
                        if (chemImmunity.Tnf.HasValue)
                            this.ChemistryImmunityTNFa.Text = chemImmunity.Tnf.ToString();
                        if (chemImmunity.Crp.HasValue)
                            this.ChemistryImmunityCRP.Text = chemImmunity.Crp.ToString();
                        if (chemImmunity.Neomycin.HasValue)
                            this.ChemistryImmunityNms.Text = chemImmunity.Neomycin.ToString();
                    }
                    if (chemistry.ChemistryLymphocyteId.HasValue)
                    {
                        this.FieldSetHospitalChemistryLbxb.Expand();
                        ChemistryLymphocyte chemLymphocyte = chemLymphocyteService.Get(chemistry.ChemistryLymphocyteId.Value);
                        if (chemLymphocyte.Cd3.HasValue)
                            this.ChemistryLbxbCD3.Text = chemLymphocyte.Cd3.ToString();
                        if (chemLymphocyte.Cd4.HasValue)
                            this.ChemistryLbxbCD4.Text = chemLymphocyte.Cd4.ToString();
                        if (chemLymphocyte.Cd8.HasValue)
                            this.ChemistryLbxbCD8.Text = chemLymphocyte.Cd8.ToString();
                        if (chemLymphocyte.Cd19.HasValue)
                            this.ChemistryLbxbCD19.Text = chemLymphocyte.Cd19.ToString();
                        if (chemLymphocyte.Cd1656.HasValue)
                            this.ChemistryLbxbCD1656.Text = chemLymphocyte.Cd1656.ToString();
                        if (chemLymphocyte.TBNk.HasValue)
                            this.ChemistryLbxbTBNK.Text = chemLymphocyte.TBNk.ToString();
                        if (chemLymphocyte.HS.HasValue)
                            this.ChemistryLbxbHS.Text = chemLymphocyte.HS.ToString();
                    }
                    if (chemistry.ChemistryBiochemistryId.HasValue)
                    {
                        this.FieldSetHospitalChemistryXsh.Expand();
                        ChemistryBiochemistry chemBiochemistry = chemBiochemistryService.Get(chemistry.ChemistryBiochemistryId.Value);
                        if (chemBiochemistry.Urea.HasValue)
                            this.ChemistryXshNS2.Text = chemBiochemistry.Urea.ToString();
                        if (chemBiochemistry.Na.HasValue)
                            this.ChemistryXshNa.Text = chemBiochemistry.Na.ToString();
                        if (chemBiochemistry.Ka.HasValue)
                            this.ChemistryXshKa.Text = chemBiochemistry.Ka.ToString();
                        if (chemBiochemistry.Cl.HasValue)
                            this.ChemistryXshCl.Text = chemBiochemistry.Cl.ToString();
                        if (chemBiochemistry.Acid.HasValue)
                            this.ChemistryXshCS.Text = chemBiochemistry.Acid.ToString();
                        if (chemBiochemistry.Ca.HasValue)
                            this.ChemistryXshCa.Text = chemBiochemistry.Ca.ToString();
                        if (chemBiochemistry.Sugar.HasValue)
                            this.ChemistryXshPTT.Text = chemBiochemistry.Sugar.ToString();
                        if (chemBiochemistry.Creatinine.HasValue)
                            this.ChemistryXshJG.Text = chemBiochemistry.Creatinine.ToString();
                        if (chemBiochemistry.UricAcid.HasValue)
                            this.ChemistryXshNS.Text = chemBiochemistry.UricAcid.ToString();
                        if (chemBiochemistry.CitricAcid.HasValue)
                            this.ChemistryXshGYS.Text = chemBiochemistry.CitricAcid.ToString();
                        if (chemBiochemistry.Phosphorus.HasValue)
                            this.ChemistryXshL.Text = chemBiochemistry.Phosphorus.ToString();
                        if (chemBiochemistry.Co2.HasValue)
                            this.ChemistryXshCO2.Text = chemBiochemistry.Co2.ToString();
                        if (chemBiochemistry.Albumin.HasValue)
                            this.ChemistryXshBDB.Text = chemBiochemistry.Albumin.ToString();
                    }
                    if (chemistry.ChemistryUrine24hId.HasValue)
                    {
                        this.FieldSetHospitalChemistry24hN.Expand();
                        ChemistryUrine24h chem24hurine = chem24hurineService.Get(chemistry.ChemistryUrine24hId.Value);
                        if (chem24hurine.Ph.HasValue)
                            this.Chemistry24hNSJD.Text = chem24hurine.Ph.ToString();
                        if (chem24hurine.Ca.HasValue)
                            this.Chemistry24hNCa.Text = chem24hurine.Ca.ToString();
                        if (chem24hurine.Na.HasValue)
                            this.Chemistry24hNNa.Text = chem24hurine.Na.ToString();
                        if (chem24hurine.Ka.HasValue)
                            this.Chemistry24hNKa.Text = chem24hurine.Ka.ToString();
                        if (chem24hurine.Cl.HasValue)
                            this.Chemistry24hNCl.Text = chem24hurine.Cl.ToString();
                        if (chem24hurine.Acid.HasValue)
                            this.Chemistry24hNCS.Text = chem24hurine.Acid.ToString();
                        if (chem24hurine.Sugar.HasValue)
                            this.Chemistry24hNPTT.Text = chem24hurine.Sugar.ToString();
                        if (chem24hurine.Creatinine.HasValue)
                            this.Chemistry24hNJG.Text = chem24hurine.Creatinine.ToString();
                        if (chem24hurine.Phosphorus.HasValue)
                            this.Chemistry24hNL.Text = chem24hurine.Phosphorus.ToString();
                        if (chem24hurine.UricAcid.HasValue)
                            this.Chemistry24hNNS.Text = chem24hurine.UricAcid.ToString();
                        if (chem24hurine.CitricAcid.HasValue)
                            this.Chemistry24hNGYS.Text = chem24hurine.CitricAcid.ToString();
                    }
                    if (chemistry.ChemistrySpermId.HasValue)
                    {
                        this.FieldSetHospitalChemistrySperm.Expand();
                        ChemistrySperm chemSperm = chemSpermService.Get(chemistry.ChemistrySpermId.Value);
                        if (chemSperm.Count.HasValue)
                            this.ChemistrySpermCount.Text = chemSperm.Count.ToString();
                        if (chemSperm.ActiveRate.HasValue)
                            this.ChemistrySpermRate.Text = chemSperm.ActiveRate.ToString();
                        this.ChemistrySpermPower.Text = chemSperm.ActivePower;
                        this.ChemistrySpermStatus.Text = chemSperm.Status;
                    }
                    if (chemistry.ChemistryPGId.HasValue)
                    {
                        this.FieldSetHospitalChemistryPG.Expand();
                        ChemistryPG chemPG = chemPGService.Get(chemistry.ChemistryPGId.Value);
                        this.ChemistryPGLecithin.Text = chemPG.Lecithin;
                        this.ChemistryPGRbc.Text = chemPG.Rbc;
                        this.ChemistryPGWbc.Text = chemPG.Wbc;
                    }
                }
                #endregion

                if (hospitalInfo.OpticsId.HasValue)
                {
                    this.CheckboxOptics.Checked = true;
                    this.PanelHospitalOptics.Disabled = false;

                    Optics optics = opticsService.Get(hospitalInfo.OpticsId.Value);
                    if (optics.TypeBUltrasonicFileId.HasValue)
                    {
                        this.HiddenOpticsBFileId.Text = optics.TypeBUltrasonicFileId.Value.ToString();
                        this.ButtonOpticsBDownload.Disabled = false;
                        this.ButtonOpticsBDelete.Disabled = false;
                    }
                    if (optics.CTFileId.HasValue)
                    {
                        this.HiddenOpticsCTFileId.Text = optics.CTFileId.Value.ToString();
                        this.ButtonOpticsCTDownload.Disabled = false;
                        this.ButtonOpticsCTDelete.Disabled = false;
                    }
                    if (optics.CTUFileId.HasValue)
                    {
                        this.HiddenOpticsCTUFileId.Text = optics.CTUFileId.Value.ToString();
                        this.ButtonOpticsCTUDownload.Disabled = false;
                        this.ButtonOpticsCTDelete.Disabled = false;
                    }
                    if (optics.KUBIVPFileId.HasValue)
                    {
                        this.HiddenOpticsKUBIVPFileId.Text = optics.KUBIVPFileId.Value.ToString();
                        this.ButtonOpticsKUBIVPDownload.Disabled = false;
                        this.ButtonOpticsKUBIVPDelete.Disabled = false;
                    }
                    if (optics.ECTFileId.HasValue)
                    {
                        this.HiddenOpticsECTFileId.Text = optics.ECTFileId.Value.ToString();
                        this.ButtonOpticsECTDownload.Disabled = false;
                        this.ButtonOpticsECTDelete.Disabled = false;
                    }

                    if (optics.CTvalue.HasValue) this.NumberFieldOpticsCT.Text = optics.CTvalue.Value.ToString();
                    if (optics.Angle.HasValue) this.NumberFieldOpticsAngle.Text = optics.Angle.Value.ToString();
                    if (optics.GFRvalue.HasValue) this.NumberFieldOpticsGFR.Text = optics.GFRvalue.Value.ToString();
                    this.TextAreaOpticsComment.Text = optics.Comment;
                }

                if (hospitalInfo.SurgeryId.HasValue)
                {
                    this.CheckboxSurgery.Checked = true;
                    this.PanelHospitalSurgery.Disabled = false;

                    Surgery surgery = surgeryService.Get(hospitalInfo.SurgeryId.Value);
                    this.DateFieldSurgery.SetValue(surgery.SurgeryDate.ToShortDateString());
                    if (surgery.KidneyPos.HasValue)
                    {
                        if (surgery.KidneyPos == 0) this.RadioGroupSurgeryStonePosition.SetValue("RadioSurgeryStoneLeft", true);
                        else if (surgery.KidneyPos == 1) this.RadioGroupSurgeryStonePosition.SetValue("RadioSurgeryStoneRight", true);
                        else this.RadioGroupSurgeryStonePosition.SetValue("RadioSurgeryStoneAll", true);
                    }
                    if (surgery.StoneSize.HasValue) this.NumberFieldSurgeryStoneSize.Text = surgery.StoneSize.Value.ToString();
                    if (surgery.RecordFileId.HasValue) this.HiddenSurgeryRecordFileId.Text = surgery.RecordFileId.Value.ToString();
                    this.TextAreaSurgeryStonePosition.Text = surgery.StonePosition;
                    this.TextAreaSurgeryStoneRemain.Text = surgery.StoneRemain;

                    if (surgery.KidneyId.HasValue)
                    {
                        this.RadioGroupSurgery.SetValue("RadioSurgeryKidney", true);

                        this.PanelSurgeryKidney.Enabled = true;
                        this.PanelSurgerySoft.Enabled = false;
                        this.PanelSurgeryHard.Enabled = false;
                        this.PanelSurgeryPG.Enabled = false;
                        this.PanelSurgerySperm.Enabled = false;

                        this.TabPanelSurgery.ActiveTabIndex = 0;

                        SurgeryKidney surgeryKidney = surgeryKidneyService.Get(surgery.KidneyId.Value);
                        if (surgeryKidney.Channel.HasValue) this.NumberFieldSurgeryKidneyChannel.Text = surgeryKidney.Channel.Value.ToString();
                        if (surgeryKidney.ChannelSize.HasValue) this.ComboBoxSurgeryKidneyChannelSize.SetValue(surgeryKidney.ChannelSize.Value);
                        if (surgeryKidney.KidneyEnter.HasValue) this.ComboBoxSurgeryKidneyEnter.SetValue(surgeryKidney.KidneyEnter.Value.ToString());

                        if ((surgeryKidney.KidneyBlood.HasValue) && (surgeryKidney.KidneyBlood.Value)) this.CheckboxSurgeryKidneyBlood.Checked = true;

                        if (surgeryKidney.Treat.HasValue)
                        {
                            if (surgeryKidney.Treat.Value) this.RadioGroupSurgeryKidneyTreat.SetValue("RadioSurgeryKidneyTreatOpen", true);
                            else this.RadioGroupSurgeryKidneyTreat.SetValue("RadioSurgeryKidneyTreatKeep", true);
                        }

                        if (surgeryKidney.DSARecordFileId.HasValue) this.HiddenSurgeryKidneyDSARecordFileId.Text = surgeryKidney.DSARecordFileId.Value.ToString();
                        if (surgeryKidney.SurgeryRecordFileId.HasValue) this.HiddenSurgeryKidneyRecordFileId.Text = surgeryKidney.SurgeryRecordFileId.Value.ToString();
                    }
                    else if (surgery.SoftTubeId.HasValue)
                    {
                        this.RadioGroupSurgery.SetValue("RadioSurgerySoft", true);

                        this.PanelSurgeryKidney.Enabled = false;
                        this.PanelSurgerySoft.Enabled = true;
                        this.PanelSurgeryHard.Enabled = false;
                        this.PanelSurgeryPG.Enabled = false;
                        this.PanelSurgerySperm.Enabled = false;

                        this.TabPanelSurgery.ActiveTabIndex = 1;

                        SurgerySofttube surgerySofttube = surgerySofttubeService.Get(surgery.SoftTubeId.Value);
                        if ((surgerySofttube.Slit.HasValue) && (surgerySofttube.Slit.Value)) this.CheckboxSurgerySoftSlit.Checked = true;
                        if (surgerySofttube.ExpansionSize.HasValue) this.ComboBoxSurgerySoftExpansion.SetValue(surgerySofttube.ExpansionSize.Value);
                        this.TextFieldSurgerySoftBasket.Text = surgerySofttube.Basket;
                        if ((surgerySofttube.Narrow.HasValue) && (surgerySofttube.Narrow.Value))
                        {
                            //this.FieldSetSurgerySoftNarrow.Expand();
                            //this.FieldSetSurgerySoftNarrow.AddScript("Ext.getCmp('FieldSetSurgerySoftNarrow').expand(false);");
                            this.CheckboxSurgerySoftNarrow.Checked = true;
                            this.TextFieldSurgerySoftNarrow.Show();
                            this.TextFieldSurgerySoftNarrow.Text = surgerySofttube.NarrowPosition;
                        }
                    }
                    else if (surgery.HardTubeId.HasValue)
                    {
                        this.RadioGroupSurgery.SetValue("RadioSurgeryHard", true);

                        this.PanelSurgeryKidney.Enabled = false;
                        this.PanelSurgerySoft.Enabled = false;
                        this.PanelSurgeryHard.Enabled = true;
                        this.PanelSurgeryPG.Enabled = false;
                        this.PanelSurgerySperm.Enabled = false;

                        this.TabPanelSurgery.ActiveTabIndex = 2;

                        SurgeryHardtube surgeryHardtube = surgeryHardtubeService.Get(surgery.HardTubeId.Value);
                        if ((surgeryHardtube.Slit.HasValue) && (surgeryHardtube.Slit.Value)) this.CheckboxSurgeryHardSlit.Checked = true;
                        if ((surgeryHardtube.Narrow.HasValue) && (surgeryHardtube.Narrow.Value))
                        {
                            //this.FieldSetSurgeryHardNarrow.Expand();
                            //this.FieldSetSurgeryHardNarrow.AddScript("Ext.getCmp('FieldSetSurgeryHardNarrow').expand(false);");
                            this.CheckboxSurgeryHardNarrow.Checked = true;
                            this.TextFieldSurgeryHardNarrow.Show();
                            this.TextFieldSurgeryHardNarrow.Text = surgeryHardtube.NarrowPosition;
                        }
                    }
                    else if (surgery.PgId.HasValue)
                    {
                        this.RadioGroupSurgery.SetValue("RadioSurgeryPG", true);

                        this.PanelSurgeryKidney.Enabled = false;
                        this.PanelSurgerySoft.Enabled = false;
                        this.PanelSurgeryHard.Enabled = false;
                        this.PanelSurgeryPG.Enabled = true;
                        this.PanelSurgerySperm.Enabled = false;

                        this.TabPanelSurgery.ActiveTabIndex = 3;

                        SurgeryPG surgeryPG = surgeryPGService.Get(surgery.PgId.Value);
                        if ((surgeryPG.Laser.HasValue) && (surgeryPG.Laser.Value)) this.RadioGroupSurgeryPG.SetValue("RadioSurgeryPGLaser", true);
                        else this.RadioGroupSurgeryPG.SetValue("RadioSurgeryPGPoint", true);
                    }
                    else if (surgery.SpermId.HasValue)
                    {
                        this.RadioGroupSurgery.SetValue("RadioSurgerySperm", true);

                        this.PanelSurgeryKidney.Enabled = false;
                        this.PanelSurgerySoft.Enabled = false;
                        this.PanelSurgeryHard.Enabled = false;
                        this.PanelSurgeryPG.Enabled = false;
                        this.PanelSurgerySperm.Enabled = true;

                        this.TabPanelSurgery.ActiveTabIndex = 4;

                        SurgerySperm surgerySperm = surgerySpermService.Get(surgery.SpermId.Value);
                        if ((surgerySperm.Ems.HasValue) && (surgerySperm.Ems.Value)) this.CheckboxSurgerySpermEms.Checked = true;
                    }

                    if (surgery.InfectionId.HasValue)
                    {
                        //this.FieldSetSurgeryInfection.Expand();
                        //this.FieldSetSurgeryInfection.AddScript("Ext.getCmp('FieldSetSurgeryInfection').expand(false);");
                        this.CheckboxSurgeryInfection.Checked = true;
                        this.panelSurgeryInfection.Show();
                        SurgeryInfection surgeryInfection = surgeryInfectionService.Get(surgery.InfectionId.Value);
                        if ((surgeryInfection.Fever.HasValue) && (surgeryInfection.Fever.Value)) this.CheckboxSurgeryInfectionFever.Checked = true;
                        if ((surgeryInfection.Blood.HasValue) && (surgeryInfection.Blood.Value)) this.CheckboxSurgeryInfectionBlood.Checked = true;
                        if ((surgeryInfection.Disease.HasValue) && (surgeryInfection.Disease.Value))
                        {
                            //this.FieldSetSurgeryInfectionDisease.Expand();
                            //this.FieldSetSurgeryInfectionDisease.AddScript("Ext.getCmp('FieldSetSurgeryInfectionDisease').expand(false);");
                            this.CheckboxSurgeryInfectionDisease.Checked = true;
                            this.panelSurgeryInfectionDisease.Show();
                            if (surgeryInfection.AntibioticsType.HasValue) this.NumberFieldSurgeryInfectionType.Text = surgeryInfection.AntibioticsType.Value.ToString();
                            if (surgeryInfection.AntibioticsTime.HasValue) this.NumberFieldSurgeryInfectionTime.Text = surgeryInfection.AntibioticsTime.Value.ToString();
                            if ((surgeryInfection.DiseaseCure.HasValue) && (surgeryInfection.DiseaseCure.Value)) this.CheckboxSurgeryInfectionCure.Checked = true;
                        }

                    }
                }

                this.TabPanelNewHospitalInfo.ActiveTabIndex = 0;
                this.WindowHospitalInfo.Title = "编辑就医信息";
                this.WindowHospitalInfo.Show();
                return true;
            }
            catch
            {
                return false;
            }
        }

        [DirectMethod]
        public bool SaveHospitalInfo()
        //protected void SaveHospitalInfo(object sender, DirectEventArgs e)
        {
            try
            {
                long hospitalId;
                if (!long.TryParse(this.HiddenHospitalId.Text, out hospitalId)) hospitalId = 0;

                Hospitalization hi;

                if (hospitalId == 0)
                {
                    hi = new Hospitalization();
                }
                else
                {
                    hi = hospitalizationService.Get(hospitalId);
                }

                DateTime dt = new DateTime(1, 1, 1);
                if (this.DateFieldHospitalInfo.SelectedDate != dt)
                {
                    hi.Date = this.DateFieldHospitalInfo.SelectedDate;
                }
                else
                {
                    X.Msg.Alert("错误", "就医时间不能为空！").Show();
                    //this.ResourceManager1.AddScript("Ext.Msg.alert('错误', '就医时间不能为空！');");
                    return false;
                }

                #region 保存化验信息
                if (this.CheckboxChemistry.Checked)
                {
                    Chemistry chemistry;
                    if (hi.ChemistryId.HasValue)
                    {
                        chemistry = chemService.Get(hi.ChemistryId.Value);
                    }
                    else
                    {
                        chemistry = new Chemistry();
                    }

                    ChemistryBlood chemBlood;
                    if (FieldSetHospitalChemistryBlood.Collapsed)
                    {
                        if (chemistry.ChemistryBloodId.HasValue)
                        {
                            if (chemBloodService.Delete(chemistry.ChemistryBloodId.Value))
                                chemistry.ChemistryBloodId = null;
                        }
                    }
                    else
                    {
                        if (chemistry.ChemistryBloodId.HasValue)
                        {
                            chemBlood = chemBloodService.Get(chemistry.ChemistryBloodId.Value);
                        }
                        else
                        {
                            chemBlood = new ChemistryBlood();
                        }

                        if (!String.IsNullOrEmpty(this.ChemistryBloodWBC.Text))
                            chemBlood.Wbc = Decimal.Parse(this.ChemistryBloodWBC.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryBloodLYMl.Text))
                            chemBlood.LymL = Decimal.Parse(this.ChemistryBloodLYMl.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryBloodM0l.Text))
                            chemBlood.M0L = Decimal.Parse(this.ChemistryBloodM0l.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryBloodGRANl.Text))
                            chemBlood.GranL = Decimal.Parse(this.ChemistryBloodGRANl.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryBloodE0l.Text))
                            chemBlood.E0L = Decimal.Parse(this.ChemistryBloodE0l.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryBloodBAS0l.Text))
                            chemBlood.Bas0L = Decimal.Parse(this.ChemistryBloodBAS0l.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryBloodLYMp.Text))
                            chemBlood.LymP = Decimal.Parse(this.ChemistryBloodLYMp.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryBloodM0p.Text))
                            chemBlood.M0P = Decimal.Parse(this.ChemistryBloodM0p.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryBloodE0p.Text))
                            chemBlood.E0P = Decimal.Parse(this.ChemistryBloodE0p.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryBloodBAS0p.Text))
                            chemBlood.Bas0P = Decimal.Parse(this.ChemistryBloodBAS0p.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryBloodGRANp.Text))
                            chemBlood.GranP = Decimal.Parse(this.ChemistryBloodGRANp.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryBloodRBC.Text))
                            chemBlood.Rbc = Decimal.Parse(this.ChemistryBloodRBC.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryBloodHGB.Text))
                            chemBlood.Hgb = Decimal.Parse(this.ChemistryBloodHGB.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryBloodHCT.Text))
                            chemBlood.Hct = Decimal.Parse(this.ChemistryBloodHCT.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryBloodPLT.Text))
                            chemBlood.Plt = Decimal.Parse(this.ChemistryBloodPLT.Text);

                        if (chemBlood.Id == 0)
                        {
                            chemBloodService.Insert(chemBlood);
                            chemistry.ChemistryBloodId = chemBlood.Id;
                        }
                        else
                        {
                            chemBloodService.Update(chemBlood);
                        }
                    }

                    ChemistryCruor chemCruor;
                    if (FieldSetHospitalChemistryCruor.Collapsed)
                    {
                        if (chemistry.ChemistryCruorId.HasValue)
                        {
                            if (chemCruorService.Delete(chemistry.ChemistryCruorId.Value))
                                chemistry.ChemistryCruorId = null;
                        }
                    }
                    else
                    {
                        if (chemistry.ChemistryCruorId.HasValue)
                        {
                            chemCruor = chemCruorService.Get(chemistry.ChemistryCruorId.Value);
                        }
                        else
                        {
                            chemCruor = new ChemistryCruor();
                        }

                        if (!String.IsNullOrEmpty(this.ChemistryCruorPT.Text))
                            chemCruor.Pt = Decimal.Parse(this.ChemistryCruorPT.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryCruorAPTT.Text))
                            chemCruor.Aptt = Decimal.Parse(this.ChemistryCruorAPTT.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryCruorTT.Text))
                            chemCruor.Tt = Decimal.Parse(this.ChemistryCruorTT.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryCruorFIB.Text))
                            chemCruor.Fib = Decimal.Parse(this.ChemistryCruorFIB.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryCruorFDP.Text))
                            chemCruor.Fdp = Decimal.Parse(this.ChemistryCruorFDP.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryCruorD2.Text))
                            chemCruor.Ddimer = Decimal.Parse(this.ChemistryCruorD2.Text);

                        if (chemCruor.Id == 0)
                        {
                            chemCruorService.Insert(chemCruor);
                            chemistry.ChemistryCruorId = chemCruor.Id;
                        }
                        else
                        {
                            chemCruorService.Update(chemCruor);
                        }
                    }

                    ChemistryUrine chemUrine;
                    if (FieldSetChemistryUrine.Collapsed)
                    {
                        if (chemistry.ChemistryUrineId.HasValue)
                        {
                            if (chemUrineService.Delete(chemistry.ChemistryUrineId.Value))
                                chemistry.ChemistryUrineId = null;
                        }
                    }
                    else
                    {
                        if (chemistry.ChemistryUrineId.HasValue)
                        {
                            chemUrine = chemUrineService.Get(chemistry.ChemistryUrineId.Value);
                        }
                        else
                        {
                            chemUrine = new ChemistryUrine();
                        }

                        if (!String.IsNullOrEmpty(this.ChemistryUrineBZ.Text))
                            chemUrine.Weight = Decimal.Parse(this.ChemistryUrineBZ.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryUrineSJD.Text))
                            chemUrine.Ph = Decimal.Parse(this.ChemistryUrineSJD.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryUrineWhite.Text))
                            chemUrine.Wbc = Decimal.Parse(this.ChemistryUrineWhite.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryUrineYXSY.Text))
                            chemUrine.Nitrite = Decimal.Parse(this.ChemistryUrineYXSY.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryUrineDBZ.Text))
                            chemUrine.Protein = Decimal.Parse(this.ChemistryUrineDBZ.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryUrineRCul.Text))
                            chemUrine.Rbcul = Decimal.Parse(this.ChemistryUrineRCul.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryUrineRChp.Text))
                            chemUrine.Rbchp = Decimal.Parse(this.ChemistryUrineRChp.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryUrineWCul.Text))
                            chemUrine.Wbcul = Decimal.Parse(this.ChemistryUrineWCul.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryUrineWChp.Text))
                            chemUrine.Wbchp = Decimal.Parse(this.ChemistryUrineWChp.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryUrineGCul.Text))
                            chemUrine.Tcul = Decimal.Parse(this.ChemistryUrineGCul.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryUrineGChp.Text))
                            chemUrine.Tchp = Decimal.Parse(this.ChemistryUrineGChp.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryUrineBLGX.Text))
                            chemUrine.Tube = Decimal.Parse(this.ChemistryUrineBLGX.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryUrineJJ.Text))
                            chemUrine.Crystal = Decimal.Parse(this.ChemistryUrineJJ.Text);

                        if (chemUrine.Id == 0)
                        {
                            chemUrineService.Insert(chemUrine);
                            chemistry.ChemistryUrineId = chemUrine.Id;
                        }
                        else
                        {
                            chemUrineService.Update(chemUrine);
                        }
                    }

                    ChemistryImmunity chemImmunity;
                    if (FieldSetHospitalChemistryMygn.Collapsed)
                    {
                        if (chemistry.ChemistryImmunityId.HasValue)
                        {
                            if (chemImmunityService.Delete(chemistry.ChemistryImmunityId.Value))
                                chemistry.ChemistryImmunityId = null;
                        }
                    }
                    else
                    {
                        if (chemistry.ChemistryImmunityId.HasValue)
                        {
                            chemImmunity = chemImmunityService.Get(chemistry.ChemistryImmunityId.Value);
                        }
                        else
                        {
                            chemImmunity = new ChemistryImmunity();
                        }

                        if (!String.IsNullOrEmpty(this.ChemistryImmunityIL2.Text))
                            chemImmunity.Il2 = Decimal.Parse(this.ChemistryImmunityIL2.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryImmunityIL6.Text))
                            chemImmunity.Il6 = Decimal.Parse(this.ChemistryImmunityIL6.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryImmunityIL8.Text))
                            chemImmunity.Il8 = Decimal.Parse(this.ChemistryImmunityIL8.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryImmunityIL10.Text))
                            chemImmunity.Il10 = Decimal.Parse(this.ChemistryImmunityIL10.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryImmunityTNFa.Text))
                            chemImmunity.Tnf = Decimal.Parse(this.ChemistryImmunityTNFa.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryImmunityCRP.Text))
                            chemImmunity.Crp = Decimal.Parse(this.ChemistryImmunityCRP.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryImmunityNms.Text))
                            chemImmunity.Neomycin = Decimal.Parse(this.ChemistryImmunityNms.Text);

                        if (chemImmunity.Id == 0)
                        {
                            chemImmunityService.Insert(chemImmunity);
                            chemistry.ChemistryImmunityId = chemImmunity.Id;
                        }
                        else
                        {
                            chemImmunityService.Update(chemImmunity);
                        }
                    }

                    ChemistryLymphocyte chemLymphocyte;
                    if (FieldSetHospitalChemistryLbxb.Collapsed)
                    {
                        if (chemistry.ChemistryLymphocyteId.HasValue)
                        {
                            if (chemLymphocyteService.Delete(chemistry.ChemistryLymphocyteId.Value))
                                chemistry.ChemistryLymphocyteId = null;
                        }
                    }
                    else
                    {
                        if (chemistry.ChemistryLymphocyteId.HasValue)
                        {
                            chemLymphocyte = chemLymphocyteService.Get(chemistry.ChemistryLymphocyteId.Value);
                        }
                        else
                        {
                            chemLymphocyte = new ChemistryLymphocyte();
                        }

                        if (!String.IsNullOrEmpty(this.ChemistryLbxbCD3.Text))
                            chemLymphocyte.Cd3 = Decimal.Parse(this.ChemistryLbxbCD3.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryLbxbCD4.Text))
                            chemLymphocyte.Cd4 = Decimal.Parse(this.ChemistryLbxbCD4.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryLbxbCD8.Text))
                            chemLymphocyte.Cd8 = Decimal.Parse(this.ChemistryLbxbCD8.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryLbxbCD19.Text))
                            chemLymphocyte.Cd19 = Decimal.Parse(this.ChemistryLbxbCD19.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryLbxbCD1656.Text))
                            chemLymphocyte.Cd1656 = Decimal.Parse(this.ChemistryLbxbCD1656.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryLbxbTBNK.Text))
                            chemLymphocyte.TBNk = Decimal.Parse(this.ChemistryLbxbTBNK.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryLbxbHS.Text))
                            chemLymphocyte.HS = Decimal.Parse(this.ChemistryLbxbHS.Text);

                        if (chemLymphocyte.Id == 0)
                        {
                            chemLymphocyteService.Insert(chemLymphocyte);
                            chemistry.ChemistryLymphocyteId = chemLymphocyte.Id;
                        }
                        else
                        {
                            chemLymphocyteService.Update(chemLymphocyte);
                        }
                    }

                    ChemistryBiochemistry chemBiochemistry;
                    if (FieldSetHospitalChemistryXsh.Collapsed)
                    {
                        if (chemistry.ChemistryBiochemistryId.HasValue)
                        {
                            if (chemBiochemistryService.Delete(chemistry.ChemistryBiochemistryId.Value))
                                chemistry.ChemistryBiochemistryId = null;
                        }
                    }
                    else
                    {
                        if (chemistry.ChemistryBiochemistryId.HasValue)
                        {
                            chemBiochemistry = chemBiochemistryService.Get(chemistry.ChemistryBiochemistryId.Value);
                        }
                        else
                        {
                            chemBiochemistry = new ChemistryBiochemistry();
                        }

                        if (!String.IsNullOrEmpty(this.ChemistryXshNS2.Text))
                            chemBiochemistry.Urea = Decimal.Parse(this.ChemistryXshNS2.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryXshNa.Text))
                            chemBiochemistry.Na = Decimal.Parse(this.ChemistryXshNa.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryXshKa.Text))
                            chemBiochemistry.Ka = Decimal.Parse(this.ChemistryXshKa.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryXshCl.Text))
                            chemBiochemistry.Cl = Decimal.Parse(this.ChemistryXshCl.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryXshCS.Text))
                            chemBiochemistry.Acid = Decimal.Parse(this.ChemistryXshCS.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryXshCa.Text))
                            chemBiochemistry.Ca = Decimal.Parse(this.ChemistryXshCa.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryXshBDB.Text))
                            chemBiochemistry.Albumin = Decimal.Parse(this.ChemistryXshBDB.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryXshPTT.Text))
                            chemBiochemistry.Sugar = Decimal.Parse(this.ChemistryXshPTT.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryXshJG.Text))
                            chemBiochemistry.Creatinine = Decimal.Parse(this.ChemistryXshJG.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryXshNS.Text))
                            chemBiochemistry.UricAcid = Decimal.Parse(this.ChemistryXshNS.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryXshGYS.Text))
                            chemBiochemistry.CitricAcid = Decimal.Parse(this.ChemistryXshGYS.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryXshL.Text))
                            chemBiochemistry.Phosphorus = Decimal.Parse(this.ChemistryXshL.Text);
                        if (!String.IsNullOrEmpty(this.ChemistryXshCO2.Text))
                            chemBiochemistry.Co2 = Decimal.Parse(this.ChemistryXshCO2.Text);

                        if (chemBiochemistry.Id == 0)
                        {
                            chemBiochemistryService.Insert(chemBiochemistry);
                            chemistry.ChemistryBiochemistryId = chemBiochemistry.Id;
                        }
                        else
                        {
                            chemBiochemistryService.Update(chemBiochemistry);
                        }
                    }

                    ChemistryUrine24h chem24hurine;
                    if (FieldSetHospitalChemistry24hN.Collapsed)
                    {
                        if (chemistry.ChemistryUrine24hId.HasValue)
                        {
                            if (chem24hurineService.Delete(chemistry.ChemistryUrine24hId.Value))
                                chemistry.ChemistryUrine24hId = null;
                        }
                    }
                    else
                    {
                        if (chemistry.ChemistryUrine24hId.HasValue)
                        {
                            chem24hurine = chem24hurineService.Get(chemistry.ChemistryUrine24hId.Value);
                        }
                        else
                        {
                            chem24hurine = new ChemistryUrine24h();
                        }

                        if (!String.IsNullOrEmpty(this.Chemistry24hNCa.Text))
                            chem24hurine.Ca = Decimal.Parse(this.Chemistry24hNCa.Text);
                        if (!String.IsNullOrEmpty(this.Chemistry24hNNa.Text))
                            chem24hurine.Na = Decimal.Parse(this.Chemistry24hNNa.Text);
                        if (!String.IsNullOrEmpty(this.Chemistry24hNKa.Text))
                            chem24hurine.Ka = Decimal.Parse(this.Chemistry24hNKa.Text);
                        if (!String.IsNullOrEmpty(this.Chemistry24hNCl.Text))
                            chem24hurine.Cl = Decimal.Parse(this.Chemistry24hNCl.Text);
                        if (!String.IsNullOrEmpty(this.Chemistry24hNCS.Text))
                            chem24hurine.Acid = Decimal.Parse(this.Chemistry24hNCS.Text);
                        if (!String.IsNullOrEmpty(this.Chemistry24hNPTT.Text))
                            chem24hurine.Sugar = Decimal.Parse(this.Chemistry24hNPTT.Text);
                        if (!String.IsNullOrEmpty(this.Chemistry24hNJG.Text))
                            chem24hurine.Creatinine = Decimal.Parse(this.Chemistry24hNJG.Text);
                        if (!String.IsNullOrEmpty(this.Chemistry24hNL.Text))
                            chem24hurine.Phosphorus = Decimal.Parse(this.Chemistry24hNL.Text);
                        if (!String.IsNullOrEmpty(this.Chemistry24hNNS.Text))
                            chem24hurine.UricAcid = Decimal.Parse(this.Chemistry24hNNS.Text);
                        if (!String.IsNullOrEmpty(this.Chemistry24hNGYS.Text))
                            chem24hurine.CitricAcid = Decimal.Parse(this.Chemistry24hNGYS.Text);
                        if (!String.IsNullOrEmpty(this.Chemistry24hNSJD.Text))
                            chem24hurine.Ph = Decimal.Parse(this.Chemistry24hNSJD.Text);

                        if (chem24hurine.Id == 0)
                        {
                            chem24hurineService.Insert(chem24hurine);
                            chemistry.ChemistryUrine24hId = chem24hurine.Id;
                        }
                        else
                        {
                            chem24hurineService.Update(chem24hurine);
                        }
                    }

                    ChemistrySperm chemSperm;
                    if (FieldSetHospitalChemistrySperm.Collapsed)
                    {
                        if (chemistry.ChemistrySpermId.HasValue)
                        {
                            if (chemSpermService.Delete(chemistry.ChemistrySpermId.Value))
                                chemistry.ChemistrySpermId = null;
                        }
                    }
                    else
                    {
                        if (chemistry.ChemistrySpermId.HasValue)
                        {
                            chemSperm = chemSpermService.Get(chemistry.ChemistrySpermId.Value);
                        }
                        else
                        {
                            chemSperm = new ChemistrySperm();
                        }

                        if (!String.IsNullOrEmpty(this.ChemistrySpermCount.Text))
                            chemSperm.Count = int.Parse(this.ChemistrySpermCount.Text);
                        if (!String.IsNullOrEmpty(this.ChemistrySpermRate.Text))
                            chemSperm.ActiveRate = int.Parse(this.ChemistrySpermRate.Text);
                        chemSperm.ActivePower = this.ChemistrySpermPower.Text;
                        chemSperm.Status = this.ChemistrySpermStatus.Text;

                        if (chemSperm.Id == 0)
                        {
                            chemSpermService.Insert(chemSperm);
                            chemistry.ChemistrySpermId = chemSperm.Id;
                        }
                        else
                        {
                            chemSpermService.Update(chemSperm);
                        }
                    }

                    ChemistryPG chemPG;
                    if (FieldSetHospitalChemistryPG.Collapsed)
                    {
                        if (chemistry.ChemistryPGId.HasValue)
                        {
                            if (chemPGService.Delete(chemistry.ChemistryPGId.Value))
                                chemistry.ChemistryPGId = null;
                        }
                    }
                    else
                    {
                        if (chemistry.ChemistryPGId.HasValue)
                        {
                            chemPG = chemPGService.Get(chemistry.ChemistryPGId.Value);
                        }
                        else
                        {
                            chemPG = new ChemistryPG();
                        }

                        chemPG.Lecithin = this.ChemistryPGLecithin.Text;
                        chemPG.Rbc = this.ChemistryPGRbc.Text;
                        chemPG.Wbc = this.ChemistryPGWbc.Text;

                        if (chemPG.Id == 0)
                        {
                            chemPGService.Insert(chemPG);
                            chemistry.ChemistryPGId = chemPG.Id;
                        }
                        else
                        {
                            chemPGService.Update(chemPG);
                        }
                    }

                    chemistry.UrineDrug = this.TextAreaHospitalChemistryZdnpyjym.Text;
                    chemistry.StoneDrug = this.TextAreaHospitalChemistryJsxjpyjym.Text;
                    chemistry.Ingredient = this.TextAreaHospitalChemistryJscffx.Text;

                    if (chemistry.Id == 0)
                    {
                        chemService.Insert(chemistry);
                        hi.ChemistryId = chemistry.Id;
                    }
                    else
                    {
                        chemService.Update(chemistry);
                    }
                }
                else
                {
                    if (hi.ChemistryId.HasValue)
                    {
                        if (chemService.DeleteChemistryInfo(chemService.Get(hi.ChemistryId.Value)))
                            hi.ChemistryId = null;
                    }
                }
                #endregion

                #region 保存光学诊断信息
                if (this.CheckboxOptics.Checked)
                {
                    Optics optics;
                    if (hi.OpticsId.HasValue)
                    {
                        optics = opticsService.Get(hi.OpticsId.Value);
                    }
                    else
                    {
                        optics = new Optics();
                    }

                    if (!String.IsNullOrEmpty(this.HiddenOpticsBFileId.Text))
                        optics.TypeBUltrasonicFileId = long.Parse(this.HiddenOpticsBFileId.Text);
                    else
                        optics.TypeBUltrasonicFileId = null;
                    if (!String.IsNullOrEmpty(this.HiddenOpticsCTFileId.Text))
                        optics.CTFileId = long.Parse(this.HiddenOpticsCTFileId.Text);
                    else
                        optics.CTFileId = null;
                    if (!String.IsNullOrEmpty(this.HiddenOpticsCTUFileId.Text))
                        optics.CTUFileId = long.Parse(this.HiddenOpticsCTUFileId.Text);
                    else
                        optics.CTUFileId = null;
                    if (!String.IsNullOrEmpty(this.HiddenOpticsKUBIVPFileId.Text))
                        optics.KUBIVPFileId = long.Parse(this.HiddenOpticsKUBIVPFileId.Text);
                    else
                        optics.KUBIVPFileId = null;
                    if (!String.IsNullOrEmpty(this.HiddenOpticsECTFileId.Text))
                        optics.ECTFileId = long.Parse(this.HiddenOpticsECTFileId.Text);
                    else
                        optics.ECTFileId = null;

                    if (!String.IsNullOrEmpty(this.NumberFieldOpticsCT.Text))
                        optics.CTvalue = int.Parse(this.NumberFieldOpticsCT.Text);
                    else
                        optics.CTvalue = null;
                    if (!String.IsNullOrEmpty(this.NumberFieldOpticsAngle.Text))
                        optics.Angle = Decimal.Parse(this.NumberFieldOpticsAngle.Text);
                    else
                        optics.Angle = null;
                    if (!String.IsNullOrEmpty(this.NumberFieldOpticsGFR.Text))
                        optics.GFRvalue = Decimal.Parse(this.NumberFieldOpticsGFR.Text);
                    else
                        optics.GFRvalue = null;
                    optics.Comment = this.TextAreaOpticsComment.Text;

                    if (optics.Id == 0)
                    {
                        opticsService.Insert(optics);
                        hi.OpticsId = optics.Id;
                    }
                    else
                    {
                        opticsService.Update(optics);
                    }
                }
                else
                {
                    if (hi.OpticsId.HasValue)
                    {
                        if (opticsService.DeleteOpticsInfo(opticsService.Get(hi.OpticsId.Value)))
                            hi.OpticsId = null; 
                    }
                }
                #endregion

                #region 保存手术信息
                if (this.CheckboxSurgery.Checked)
                {
                    Surgery surgery;
                    if (hi.SurgeryId.HasValue)
                    {
                        surgery = surgeryService.Get(hi.SurgeryId.Value);
                    }
                    else
                    {
                        surgery = new Surgery();
                    }

                    if (this.DateFieldSurgery.SelectedDate != dt)
                    {
                        surgery.SurgeryDate = this.DateFieldSurgery.SelectedDate;
                    }

                    if (this.RadioSurgeryStoneLeft.Checked) surgery.KidneyPos = 0;
                    else if (this.RadioSurgeryStoneRight.Checked) surgery.KidneyPos = 1;
                    else if (this.RadioSurgeryStoneAll.Checked) surgery.KidneyPos = 2;

                    if (this.NumberFieldSurgeryStoneSize.Text != "") surgery.StoneSize = Decimal.Parse(this.NumberFieldSurgeryStoneSize.Text);
                    surgery.StonePosition = this.TextAreaSurgeryStonePosition.Text;
                    surgery.StoneRemain = this.TextAreaSurgeryStoneRemain.Text;

                    if (!String.IsNullOrEmpty(this.HiddenSurgeryRecordFileId.Text))
                        surgery.RecordFileId = long.Parse(this.HiddenSurgeryRecordFileId.Text);
                    else
                        surgery.RecordFileId = null;

                    if (this.RadioSurgeryKidney.Checked)
                    {
                        if (surgery.SoftTubeId.HasValue)
                        {
                            if (surgerySofttubeService.Delete(surgery.SoftTubeId.Value))
                                surgery.SoftTubeId = null;
                        }
                        if (surgery.HardTubeId.HasValue)
                        {
                            if (surgeryHardtubeService.Delete(surgery.HardTubeId.Value))
                                surgery.HardTubeId = null;
                        }
                        if (surgery.PgId.HasValue)
                        {
                            if (surgeryPGService.Delete(surgery.PgId.Value))
                                surgery.PgId = null;
                        }
                        if (surgery.SpermId.HasValue)
                        {
                            if (surgerySpermService.Delete(surgery.SpermId.Value))
                                surgery.SpermId = null;
                        }

                        SurgeryKidney surgeryKidney;
                        if (surgery.KidneyId.HasValue)
                        {
                            surgeryKidney = surgeryKidneyService.Get(surgery.KidneyId.Value);
                        }
                        else
                        {
                            surgeryKidney = new SurgeryKidney();
                        }

                        surgeryKidney.Channel = (this.NumberFieldSurgeryKidneyChannel.Text == "") ? 0 : int.Parse(this.NumberFieldSurgeryKidneyChannel.Text);
                        if (this.ComboBoxSurgeryKidneyChannelSize.SelectedItem.Text == "")
                            surgeryKidney.ChannelSize = null;
                        else
                            surgeryKidney.ChannelSize = int.Parse(this.ComboBoxSurgeryKidneyChannelSize.SelectedItem.Text);

                        if (this.ComboBoxSurgeryKidneyEnter.SelectedItem.Value != "")
                            surgeryKidney.KidneyEnter = (KidneyEntranceEnum)Enum.Parse(typeof(KidneyEntranceEnum), this.ComboBoxSurgeryKidneyEnter.SelectedItem.Value);

                        if (this.CheckboxSurgeryKidneyBlood.Checked) surgeryKidney.KidneyBlood = true;
                        else surgeryKidney.KidneyBlood = false;

                        if (RadioSurgeryKidneyTreatKeep.Checked) surgeryKidney.Treat = false;
                        else surgeryKidney.Treat = true;

                        if (!String.IsNullOrEmpty(this.HiddenSurgeryKidneyDSARecordFileId.Text))
                            surgeryKidney.DSARecordFileId = long.Parse(this.HiddenSurgeryKidneyDSARecordFileId.Text);
                        else
                            surgeryKidney.DSARecordFileId = null;
                        if (!String.IsNullOrEmpty(this.HiddenSurgeryKidneyRecordFileId.Text))
                            surgeryKidney.SurgeryRecordFileId = long.Parse(this.HiddenSurgeryKidneyRecordFileId.Text);
                        else
                            surgeryKidney.SurgeryRecordFileId = null;

                        if (surgeryKidney.Id == 0)
                        {
                            surgeryKidneyService.Insert(surgeryKidney);
                            surgery.KidneyId = surgeryKidney.Id;
                        }
                        else
                        {
                            surgeryKidneyService.Update(surgeryKidney);
                        }
                    }
                    else if (this.RadioSurgerySoft.Checked)
                    {
                        if (surgery.KidneyId.HasValue)
                        {
                            if (surgeryKidneyService.Delete(surgery.KidneyId.Value))
                                surgery.KidneyId = null;
                        }
                        if (surgery.HardTubeId.HasValue)
                        {
                            if (surgeryHardtubeService.Delete(surgery.HardTubeId.Value))
                                surgery.HardTubeId = null;
                        }
                        if (surgery.PgId.HasValue)
                        {
                            if (surgeryPGService.Delete(surgery.PgId.Value))
                                surgery.PgId = null;
                        }
                        if (surgery.SpermId.HasValue)
                        {
                            if (surgerySpermService.Delete(surgery.SpermId.Value))
                                surgery.SpermId = null;
                        }

                        SurgerySofttube surgerySofttube;
                        if (surgery.SoftTubeId.HasValue)
                        {
                            surgerySofttube = surgerySofttubeService.Get(surgery.SoftTubeId.Value);
                        }
                        else
                        {
                            surgerySofttube = new SurgerySofttube();
                        }

                        if (this.CheckboxSurgerySoftSlit.Checked) surgerySofttube.Slit = true;
                        else surgerySofttube.Slit = false;

                        surgerySofttube.ExpansionSize = int.Parse(this.ComboBoxSurgerySoftExpansion.SelectedItem.Value);
                        surgerySofttube.Basket = this.TextFieldSurgerySoftBasket.Text;
                        if (this.CheckboxSurgerySoftNarrow.Checked)
                        {
                            surgerySofttube.Narrow = true;
                            surgerySofttube.NarrowPosition = this.TextFieldSurgerySoftNarrow.Text;
                        }
                        else
                        {
                            surgerySofttube.Narrow = false;
                            surgerySofttube.NarrowPosition = "";
                        }

                        if (surgerySofttube.Id == 0)
                        {
                            surgerySofttubeService.Insert(surgerySofttube);
                            surgery.SoftTubeId = surgerySofttube.Id;
                        }
                        else
                        {
                            surgerySofttubeService.Update(surgerySofttube);
                        }
                    }
                    else if (this.RadioSurgeryHard.Checked)
                    {
                        if (surgery.KidneyId.HasValue)
                        {
                            if (surgeryKidneyService.Delete(surgery.KidneyId.Value))
                                surgery.KidneyId = null;
                        }
                        if (surgery.SoftTubeId.HasValue)
                        {
                            if (surgerySofttubeService.Delete(surgery.SoftTubeId.Value))
                                surgery.SoftTubeId = null;
                        }
                        if (surgery.PgId.HasValue)
                        {
                            if (surgeryPGService.Delete(surgery.PgId.Value))
                                surgery.PgId = null;
                        }
                        if (surgery.SpermId.HasValue)
                        {
                            if (surgerySpermService.Delete(surgery.SpermId.Value))
                                surgery.SpermId = null;
                        }

                        SurgeryHardtube surgeryHardtube;
                        if (surgery.HardTubeId.HasValue)
                        {
                            surgeryHardtube = surgeryHardtubeService.Get(surgery.HardTubeId.Value);
                        }
                        else
                        {
                            surgeryHardtube = new SurgeryHardtube();
                        }

                        if (this.CheckboxSurgeryHardSlit.Checked) surgeryHardtube.Slit = true;
                        else surgeryHardtube.Slit = false;

                        if (this.CheckboxSurgeryHardNarrow.Checked)
                        {
                            surgeryHardtube.Narrow = true;
                            surgeryHardtube.NarrowPosition = this.TextFieldSurgeryHardNarrow.Text;
                        }
                        else
                        {
                            surgeryHardtube.Narrow = false;
                            surgeryHardtube.NarrowPosition = "";
                        }

                        if (surgeryHardtube.Id == 0)
                        {
                            surgeryHardtubeService.Insert(surgeryHardtube);
                            surgery.HardTubeId = surgeryHardtube.Id;
                        }
                        else
                        {
                            surgeryHardtubeService.Update(surgeryHardtube);
                        }
                    }
                    else if (this.RadioSurgeryPG.Checked)
                    {
                        if (surgery.KidneyId.HasValue)
                        {
                            if (surgeryKidneyService.Delete(surgery.KidneyId.Value))
                                surgery.KidneyId = null;
                        }
                        if (surgery.HardTubeId.HasValue)
                        {
                            if (surgeryHardtubeService.Delete(surgery.HardTubeId.Value))
                                surgery.HardTubeId = null;
                        }
                        if (surgery.SoftTubeId.HasValue)
                        {
                            if (surgerySofttubeService.Delete(surgery.SoftTubeId.Value))
                                surgery.SoftTubeId = null;
                        }
                        if (surgery.SpermId.HasValue)
                        {
                            if (surgerySpermService.Delete(surgery.SpermId.Value))
                                surgery.SpermId = null;
                        }

                        SurgeryPG surgeryPG;
                        if (surgery.PgId.HasValue)
                        {
                            surgeryPG = surgeryPGService.Get(surgery.PgId.Value);
                        }
                        else
                        {
                            surgeryPG = new SurgeryPG();
                        }

                        if (this.RadioSurgeryPGPoint.Checked) surgeryPG.Laser = false;
                        else surgeryPG.Laser = true;

                        if (surgeryPG.Id == 0)
                        {
                            surgeryPGService.Insert(surgeryPG);
                            surgery.PgId = surgeryPG.Id;
                        }
                        else
                        {
                            surgeryPGService.Update(surgeryPG);
                        }
                    }
                    else if (this.RadioSurgerySperm.Checked)
                    {
                        if (surgery.KidneyId.HasValue)
                        {
                            if (surgeryKidneyService.Delete(surgery.KidneyId.Value))
                                surgery.KidneyId = null;
                        }
                        if (surgery.HardTubeId.HasValue)
                        {
                            if (surgeryHardtubeService.Delete(surgery.HardTubeId.Value))
                                surgery.HardTubeId = null;
                        }
                        if (surgery.SoftTubeId.HasValue)
                        {
                            if (surgerySofttubeService.Delete(surgery.SoftTubeId.Value))
                                surgery.SoftTubeId = null;
                        }
                        if (surgery.PgId.HasValue)
                        {
                            if (surgeryPGService.Delete(surgery.PgId.Value))
                                surgery.PgId = null;
                        }

                        SurgerySperm surgerySperm;
                        if (surgery.SpermId.HasValue)
                        {
                            surgerySperm = surgerySpermService.Get(surgery.SpermId.Value);
                        }
                        else
                        {
                            surgerySperm = new SurgerySperm();
                        }

                        if (this.CheckboxSurgerySpermEms.Checked) surgerySperm.Ems = true;
                        else surgerySperm.Ems = false;

                        if (surgerySperm.Id == 0)
                        {
                            surgerySpermService.Insert(surgerySperm);
                            surgery.SpermId = surgerySperm.Id;
                        }
                        else
                        {
                            surgerySpermService.Update(surgerySperm);
                        }
                    }

                    SurgeryInfection surgeryInfect;
                    //if (this.FieldSetSurgeryInfection.Collapsed)
                    if(!this.CheckboxSurgeryInfection.Checked)
                    {
                        if (surgery.InfectionId.HasValue)
                        {
                            surgeryInfectionService.Delete(surgery.InfectionId.Value);
                            surgery.InfectionId = null;
                        }
                    }
                    else
                    {
                        if (surgery.InfectionId.HasValue)
                        {
                            surgeryInfect = surgeryInfectionService.Get(surgery.InfectionId.Value);
                        }
                        else
                        {
                            surgeryInfect = new SurgeryInfection();
                        }

                        if (this.CheckboxSurgeryInfectionFever.Checked) surgeryInfect.Fever = true;
                        else surgeryInfect.Fever = false;

                        if (this.CheckboxSurgeryInfectionBlood.Checked) surgeryInfect.Blood = true;
                        else surgeryInfect.Blood = false;

                        //if (this.FieldSetSurgeryInfectionDisease.Collapsed)
                        if(!this.CheckboxSurgeryInfectionDisease.Checked)
                        {
                            surgeryInfect.Disease = false;
                            surgeryInfect.AntibioticsType = null;
                            surgeryInfect.AntibioticsTime = null;
                            surgeryInfect.DiseaseCure = null;
                        }
                        else
                        {
                            surgeryInfect.Disease = true;
                            if (this.NumberFieldSurgeryInfectionType.Text != "") surgeryInfect.AntibioticsType = int.Parse(this.NumberFieldSurgeryInfectionType.Text);
                            if (this.NumberFieldSurgeryInfectionTime.Text != "") surgeryInfect.AntibioticsTime = int.Parse(this.NumberFieldSurgeryInfectionTime.Text);
                            if (this.CheckboxSurgeryInfectionCure.Checked) surgeryInfect.DiseaseCure = true;
                            else surgeryInfect.DiseaseCure = false;
                        }

                        if (surgeryInfect.Id == 0)
                        {
                            surgeryInfectionService.Insert(surgeryInfect);
                            surgery.InfectionId = surgeryInfect.Id;
                        }
                        else
                        {
                            surgeryInfectionService.Update(surgeryInfect);
                        }
                    }

                    if (surgery.Id == 0)
                    {
                        surgeryService.Insert(surgery);
                        hi.SurgeryId = surgery.Id;
                    }
                    else
                    {
                        surgeryService.Update(surgery);
                    }
                }
                else
                {
                    if (hi.SurgeryId.HasValue)
                    {
                        surgeryService.DeleteSurgeryInfo(surgeryService.Get(hi.SurgeryId.Value));
                        hi.SurgeryId = null;
                    }
                }
                #endregion

                hi.PatientId = long.Parse(this.patientId.Text);

                if (hi.Id == 0)
                {
                    hospitalizationService.Insert(hi);
                }
                else
                {
                    hospitalizationService.Update(hi);
                }

                this.WindowHospitalInfo.Hide();
                //this.StoreHospital_RefreshData(null, new StoreRefreshDataEventArgs());

                X.Msg.Alert("成功", "保存病人就医信息成功！").Show();
                return true;
            }
            catch
            {
                X.Msg.Alert("错误", "保存病人就医信息失败！").Show();
                return false;
            }
        }

        #region 上传下载文件
        [DirectMethod]
        public bool UploadFileOpticsB()
        {
            try
            {
                 MediaFile file;
                 if (String.IsNullOrEmpty(this.HiddenOpticsBFileId.Text))
                     file = new MediaFile();
                 else
                 {
                     file = mediaFileService.Get(long.Parse(this.HiddenOpticsBFileId.Text));
                     if (File.Exists(file.Path)) File.Delete(file.Path);
                 }

                file.DisplayName = this.FileUploadFieldOpticsB.PostedFile.FileName;
                file.Name = mediaFileService.GetMediaFileName() + file.DisplayName.Substring(file.DisplayName.LastIndexOf('.') );
                file.Path = Stone.Services.Application.Instance.AppSettings.ResPath + file.Name;
                file.PicVideo = false;
                file.Length = this.FileUploadFieldOpticsB.PostedFile.ContentLength;
                this.FileUploadFieldOpticsB.PostedFile.SaveAs(file.Path);

                if (file.Id == 0)
                {
                    mediaFileService.Insert(file);
                    this.HiddenOpticsBFileId.Text = file.Id.ToString();
                }
                else
                {
                    mediaFileService.Update(file);
                }

                this.ButtonOpticsBDelete.Disabled = false;
                this.ButtonOpticsBDownload.Disabled = false;
                return true;
            }
            catch
            {
                return false;
            }
        }

        [DirectMethod]
        public void DownloadFileOpticsB()
        {
            MediaFile file;
            if(String.IsNullOrEmpty(this.HiddenOpticsBFileId.Text))return;
            file = mediaFileService.Get(long.Parse(this.HiddenOpticsBFileId.Text));
            
            Page.Response.Clear();
            bool success = ResponseFile(Page.Request, Page.Response, file.DisplayName, file.Path, 1024000);
            if (!success) X.Msg.Alert("错误", "下载文件出错！") ;
        }

        [DirectMethod]
        public void DeleteOpticsB()
        {
            if(String.IsNullOrEmpty(this.HiddenOpticsBFileId.Text))return;
            MediaFile file = mediaFileService.Get(long.Parse(this.HiddenOpticsBFileId.Text));
            if (File.Exists(file.Path)) File.Delete(file.Path);

            this.ButtonOpticsBDownload.Disabled = true;
            this.ButtonOpticsBDelete.Disabled = true;
            this.HiddenOpticsBFileId.Text = null;
        }


        [DirectMethod]
        public bool UploadFileOpticsCT()
        {
            try
            {
                 MediaFile file;
                 if (String.IsNullOrEmpty(this.HiddenOpticsCTFileId.Text))
                     file = new MediaFile();
                 else
                     file = mediaFileService.Get(long.Parse(this.HiddenOpticsCTFileId.Text));

                file.DisplayName = this.FileUploadFieldOpticsCT.PostedFile.FileName;
                file.Name = mediaFileService.GetMediaFileName() + file.DisplayName.Substring(file.DisplayName.LastIndexOf('.') );
                file.Path = Stone.Services.Application.Instance.AppSettings.ResPath + file.Name;
                file.PicVideo = false;
                file.Length = this.FileUploadFieldOpticsCT.PostedFile.ContentLength;
                this.FileUploadFieldOpticsCT.PostedFile.SaveAs(file.Path);

                if (file.Id == 0)
                {
                    mediaFileService.Insert(file);
                    this.HiddenOpticsCTFileId.Text = file.Id.ToString();
                }
                else
                {
                    mediaFileService.Update(file);
                }

                this.ButtonOpticsCTDelete.Disabled = false;
                this.ButtonOpticsCTDownload.Disabled = false;
                return true;
            }
            catch
            {
                return false;
            }
        }

        [DirectMethod]
        public void DownloadFileOpticsCT()
        {
            MediaFile file;
            if(String.IsNullOrEmpty(this.HiddenOpticsCTFileId.Text))return;
            file = mediaFileService.Get(long.Parse(this.HiddenOpticsCTFileId.Text));
            
            Page.Response.Clear();
            bool success = ResponseFile(Page.Request, Page.Response, file.DisplayName, file.Path, 1024000);
            if (!success) X.Msg.Alert("错误", "下载文件出错！") ;
        }

        [DirectMethod]
        public void DeleteOpticsCT()
        {
            if (String.IsNullOrEmpty(this.HiddenOpticsCTFileId.Text)) return;
            MediaFile file = mediaFileService.Get(long.Parse(this.HiddenOpticsCTFileId.Text));
            if (File.Exists(file.Path)) File.Delete(file.Path);

            this.ButtonOpticsCTDelete.Disabled = true;
            this.ButtonOpticsCTDownload.Disabled = true;
            this.HiddenOpticsCTFileId.Text = null;
        }


        [DirectMethod]
        public bool UploadFileOpticsCTU()
        {
            try
            {
                 MediaFile file;
                 if (String.IsNullOrEmpty(this.HiddenOpticsCTUFileId.Text))
                     file = new MediaFile();
                 else
                     file = mediaFileService.Get(long.Parse(this.HiddenOpticsCTUFileId.Text));

                file.DisplayName = this.FileUploadFieldOpticsCTU.PostedFile.FileName;
                file.Name = mediaFileService.GetMediaFileName() + file.DisplayName.Substring(file.DisplayName.LastIndexOf('.') );
                file.Path = Stone.Services.Application.Instance.AppSettings.ResPath + file.Name;
                file.PicVideo = false;
                file.Length = this.FileUploadFieldOpticsCTU.PostedFile.ContentLength;
                this.FileUploadFieldOpticsCTU.PostedFile.SaveAs(file.Path);

                if (file.Id == 0)
                {
                    mediaFileService.Insert(file);
                    this.HiddenOpticsCTUFileId.Text = file.Id.ToString();
                }
                else
                {
                    mediaFileService.Update(file);
                }

                this.ButtonOpticsCTUDelete.Disabled = false;
                this.ButtonOpticsCTUDownload.Disabled = false;
                return true;
            }
            catch
            {
                return false;
            }
        }

        [DirectMethod]
        public void DownloadFileOpticsCTU()
        {
            MediaFile file;
            if(String.IsNullOrEmpty(this.HiddenOpticsCTUFileId.Text))return;
            file = mediaFileService.Get(long.Parse(this.HiddenOpticsCTUFileId.Text));
            
            Page.Response.Clear();
            bool success = ResponseFile(Page.Request, Page.Response, file.DisplayName, file.Path, 1024000);
            if (!success) X.Msg.Alert("错误", "下载文件出错！") ;
        }

        [DirectMethod]
        public void DeleteOpticsCTU()
        {
            if (String.IsNullOrEmpty(this.HiddenOpticsCTUFileId.Text)) return;
            MediaFile file = mediaFileService.Get(long.Parse(this.HiddenOpticsCTUFileId.Text));
            if (File.Exists(file.Path)) File.Delete(file.Path);

            this.ButtonOpticsCTUDelete.Disabled = true;
            this.ButtonOpticsCTUDownload.Disabled = true;
            this.HiddenOpticsCTUFileId.Text = null;
        }


        [DirectMethod]
        public bool UploadFileOpticsKUBIVP()
        {
            try
            {
                 MediaFile file;
                 if (String.IsNullOrEmpty(this.HiddenOpticsKUBIVPFileId.Text))
                     file = new MediaFile();
                 else
                     file = mediaFileService.Get(long.Parse(this.HiddenOpticsKUBIVPFileId.Text));

                file.DisplayName = this.FileUploadFieldOpticsKUBIVP.PostedFile.FileName;
                file.Name = mediaFileService.GetMediaFileName() + file.DisplayName.Substring(file.DisplayName.LastIndexOf('.') );
                file.Path = Stone.Services.Application.Instance.AppSettings.ResPath + file.Name;
                file.PicVideo = false;
                file.Length = this.FileUploadFieldOpticsKUBIVP.PostedFile.ContentLength;
                this.FileUploadFieldOpticsKUBIVP.PostedFile.SaveAs(file.Path);

                if (file.Id == 0)
                {
                    mediaFileService.Insert(file);
                    this.HiddenOpticsKUBIVPFileId.Text = file.Id.ToString();
                }
                else
                {
                    mediaFileService.Update(file);
                }

                this.ButtonOpticsKUBIVPDelete.Disabled = false;
                this.ButtonOpticsKUBIVPDownload.Disabled = false;
                return true;
            }
            catch
            {
                return false;
            }
        }

        [DirectMethod]
        public void DownloadFileOpticsKUBIVP()
        {
            MediaFile file;
            if(String.IsNullOrEmpty(this.HiddenOpticsKUBIVPFileId.Text))return;
            file = mediaFileService.Get(long.Parse(this.HiddenOpticsKUBIVPFileId.Text));
            
            Page.Response.Clear();
            bool success = ResponseFile(Page.Request, Page.Response, file.DisplayName, file.Path, 1024000);
            if (!success) X.Msg.Alert("错误", "下载文件出错！") ;
        }

        [DirectMethod]
        public void DeleteOpticsKUBIVP()
        {
            if (String.IsNullOrEmpty(this.HiddenOpticsKUBIVPFileId.Text)) return;
            MediaFile file = mediaFileService.Get(long.Parse(this.HiddenOpticsKUBIVPFileId.Text));
            if (File.Exists(file.Path)) File.Delete(file.Path);

            this.ButtonOpticsKUBIVPDelete.Disabled = true;
            this.ButtonOpticsKUBIVPDownload.Disabled = true;
            this.HiddenOpticsKUBIVPFileId.Text = null;
        }


        [DirectMethod]
        public bool UploadFileOpticsECT()
        {
            try
            {
                 MediaFile file;
                 if (String.IsNullOrEmpty(this.HiddenOpticsECTFileId.Text))
                     file = new MediaFile();
                 else
                     file = mediaFileService.Get(long.Parse(this.HiddenOpticsECTFileId.Text));

                file.DisplayName = this.FileUploadFieldOpticsECT.PostedFile.FileName;
                file.Name = mediaFileService.GetMediaFileName() + file.DisplayName.Substring(file.DisplayName.LastIndexOf('.') );
                file.Path = Stone.Services.Application.Instance.AppSettings.ResPath + file.Name;
                file.PicVideo = false;
                file.Length = this.FileUploadFieldOpticsECT.PostedFile.ContentLength;
                this.FileUploadFieldOpticsECT.PostedFile.SaveAs(file.Path);

                if (file.Id == 0)
                {
                    mediaFileService.Insert(file);
                    this.HiddenOpticsECTFileId.Text = file.Id.ToString();
                }
                else
                {
                    mediaFileService.Update(file);
                }

                this.ButtonOpticsECTDelete.Disabled = false;
                this.ButtonOpticsECTDownload.Disabled = false;
                return true;
            }
            catch
            {
                return false;
            }
        }

        [DirectMethod]
        public void DownloadFileOpticsECT()
        {
            MediaFile file;
            if(String.IsNullOrEmpty(this.HiddenOpticsECTFileId.Text))return;
            file = mediaFileService.Get(long.Parse(this.HiddenOpticsECTFileId.Text));
            
            Page.Response.Clear();
            bool success = ResponseFile(Page.Request, Page.Response, file.DisplayName, file.Path, 1024000);
            if (!success) X.Msg.Alert("错误", "下载文件出错！") ;
        }

        [DirectMethod]
        public void DeleteOpticsECT()
        {
            if (String.IsNullOrEmpty(this.HiddenOpticsECTFileId.Text)) return;
            MediaFile file = mediaFileService.Get(long.Parse(this.HiddenOpticsECTFileId.Text));
            if (File.Exists(file.Path)) File.Delete(file.Path);

            this.ButtonOpticsECTDelete.Disabled = true;
            this.ButtonOpticsECTDownload.Disabled = true;
            this.HiddenOpticsECTFileId.Text = null;
        }


        [DirectMethod]
        public bool UploadFileSurgeryRecord()
        {
            try
            {
                 MediaFile file;
                 if (String.IsNullOrEmpty(this.HiddenSurgeryRecordFileId.Text))
                     file = new MediaFile();
                 else
                     file = mediaFileService.Get(long.Parse(this.HiddenSurgeryRecordFileId.Text));

                file.DisplayName = this.FileUploadFieldSurgeryRecord.PostedFile.FileName;
                file.Name = mediaFileService.GetMediaFileName() + file.DisplayName.Substring(file.DisplayName.LastIndexOf('.') );
                file.Path = Stone.Services.Application.Instance.AppSettings.ResPath + file.Name;
                file.PicVideo = true;
                file.Length = this.FileUploadFieldSurgeryRecord.PostedFile.ContentLength;
                this.FileUploadFieldSurgeryRecord.PostedFile.SaveAs(file.Path);

                if (file.Id == 0)
                {
                    mediaFileService.Insert(file);
                    this.HiddenSurgeryRecordFileId.Text = file.Id.ToString();
                }
                else
                {
                    mediaFileService.Update(file);
                }

                this.ButtonSurgeryRecordDelete.Disabled = false;
                this.ButtonSurgeryRecordDownload.Disabled = false;
                return true;
            }
            catch
            {
                return false;
            }
        }

        [DirectMethod]
        public void DownloadFileSurgeryRecord()
        {
            MediaFile file;
            if(String.IsNullOrEmpty(this.HiddenSurgeryRecordFileId.Text))return;
            file = mediaFileService.Get(long.Parse(this.HiddenSurgeryRecordFileId.Text));
            
            Page.Response.Clear();
            bool success = ResponseFile(Page.Request, Page.Response, file.DisplayName, file.Path, 1024000);
            if (!success) X.Msg.Alert("错误", "下载文件出错！") ;
        }

        [DirectMethod]
        public void DeleteSurgeryRecord()
        {
            if (String.IsNullOrEmpty(this.HiddenSurgeryRecordFileId.Text)) return;
            MediaFile file = mediaFileService.Get(long.Parse(this.HiddenSurgeryRecordFileId.Text));
            if (File.Exists(file.Path)) File.Delete(file.Path);

            this.ButtonSurgeryRecordDelete.Disabled = true;
            this.ButtonSurgeryRecordDownload.Disabled = true;
            this.HiddenSurgeryRecordFileId.Text = null;
        }


        [DirectMethod]
        public bool UploadFileSurgeryKidneyDSARecord()
        {
            try
            {
                 MediaFile file;
                 if (String.IsNullOrEmpty(this.HiddenSurgeryKidneyDSARecordFileId.Text))
                     file = new MediaFile();
                 else
                     file = mediaFileService.Get(long.Parse(this.HiddenSurgeryKidneyDSARecordFileId.Text));

                file.DisplayName = this.FileUploadFieldSurgeryKidneyDSARecord.PostedFile.FileName;
                file.Name = mediaFileService.GetMediaFileName() + file.DisplayName.Substring(file.DisplayName.LastIndexOf('.') );
                file.Path = Stone.Services.Application.Instance.AppSettings.ResPath + file.Name;
                file.PicVideo = true;
                file.Length = this.FileUploadFieldSurgeryKidneyDSARecord.PostedFile.ContentLength;
                this.FileUploadFieldSurgeryKidneyDSARecord.PostedFile.SaveAs(file.Path);

                if (file.Id == 0)
                {
                    mediaFileService.Insert(file);
                    this.HiddenSurgeryKidneyDSARecordFileId.Text = file.Id.ToString();
                }
                else
                {
                    mediaFileService.Update(file);
                }

                this.ButtonSurgeryKidneyDSARecordDelete.Disabled = false;
                this.ButtonSurgeryKidneyDSARecordDownload.Disabled = false;
                return true;
            }
            catch
            {
                return false;
            }
        }

        [DirectMethod]
        public void DownloadFileSurgeryKidneyDSARecord()
        {
            MediaFile file;
            if(String.IsNullOrEmpty(this.HiddenSurgeryKidneyDSARecordFileId.Text))return;
            file = mediaFileService.Get(long.Parse(this.HiddenSurgeryKidneyDSARecordFileId.Text));
            
            Page.Response.Clear();
            bool success = ResponseFile(Page.Request, Page.Response, file.DisplayName, file.Path, 1024000);
            if (!success) X.Msg.Alert("错误", "下载文件出错！") ;
        }

        [DirectMethod]
        public void DeleteSurgeryKidneyDSARecord()
        {
            if (String.IsNullOrEmpty(this.HiddenSurgeryKidneyDSARecordFileId.Text)) return;
            MediaFile file = mediaFileService.Get(long.Parse(this.HiddenSurgeryKidneyDSARecordFileId.Text));
            if (File.Exists(file.Path)) File.Delete(file.Path);

            this.ButtonSurgeryKidneyDSARecordDelete.Disabled = true;
            this.ButtonSurgeryKidneyDSARecordDownload.Disabled = true;
            this.HiddenSurgeryKidneyDSARecordFileId.Text = null;
        }


        [DirectMethod]
        public bool UploadFileSurgeryKidneyRecord()
        {
            try
            {
                 MediaFile file;
                 if (String.IsNullOrEmpty(this.HiddenSurgeryKidneyRecordFileId.Text))
                     file = new MediaFile();
                 else
                     file = mediaFileService.Get(long.Parse(this.HiddenSurgeryKidneyRecordFileId.Text));

                file.DisplayName = this.FileUploadFieldSurgeryKidneyRecord.PostedFile.FileName;
                file.Name = mediaFileService.GetMediaFileName() + file.DisplayName.Substring(file.DisplayName.LastIndexOf('.') );
                file.Path = Stone.Services.Application.Instance.AppSettings.ResPath + file.Name;
                file.PicVideo = true;
                file.Length = this.FileUploadFieldSurgeryKidneyRecord.PostedFile.ContentLength;
                this.FileUploadFieldSurgeryKidneyRecord.PostedFile.SaveAs(file.Path);

                if (file.Id == 0)
                {
                    mediaFileService.Insert(file);
                    this.HiddenSurgeryKidneyRecordFileId.Text = file.Id.ToString();
                }
                else
                {
                    mediaFileService.Update(file);
                }

                this.ButtonSurgeryKidneyRecordDelete.Disabled = false;
                this.ButtonSurgeryKidneyRecordDownload.Disabled = false;
                return true;
            }
            catch
            {
                return false;
            }
        }

        [DirectMethod]
        public void DownloadFileSurgeryKidneyRecord()
        {
            MediaFile file;
            if(String.IsNullOrEmpty(this.HiddenSurgeryKidneyRecordFileId.Text))return;
            file = mediaFileService.Get(long.Parse(this.HiddenSurgeryKidneyRecordFileId.Text));
            
            Page.Response.Clear();
            bool success = ResponseFile(Page.Request, Page.Response, file.DisplayName, file.Path, 1024000);
            if (!success) X.Msg.Alert("错误", "下载文件出错！") ;
        }

        [DirectMethod]
        public void DeleteSurgeryKidneyRecord()
        {
            if (String.IsNullOrEmpty(this.HiddenSurgeryKidneyRecordFileId.Text)) return;
            MediaFile file = mediaFileService.Get(long.Parse(this.HiddenSurgeryKidneyRecordFileId.Text));
            if (File.Exists(file.Path)) File.Delete(file.Path);

            this.ButtonSurgeryKidneyRecordDelete.Disabled = true;
            this.ButtonSurgeryKidneyRecordDownload.Disabled = true;
            this.HiddenSurgeryKidneyRecordFileId.Text = null;
        }

        #endregion

        protected void StoreHospital_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            long patientId;
            if (!long.TryParse(this.patientId.Text, out patientId)) patientId = 0;

            if (patientId == 0) return;

            string sortDir = e.Dir == Ext.Net.SortDirection.Default ? "" : " " + e.Dir.ToString();
            string orderBy = "date" + sortDir;
            //switch (e.Sort)
            //{
            //    case "name":
            //        orderBy = "name" + sortDir;
            //        break;
            //    case "sex":
            //        orderBy = "sex" + sortDir;
            //        break;
            //    case "birth":
            //        orderBy = "birthday" + sortDir;
            //        break;
            //    case "age":
            //        orderBy = "age" + sortDir;
            //        break;
            //    case "no":
            //        orderBy = "number" + sortDir;
            //        break;
            //    case "height":
            //        orderBy = "height" + sortDir;
            //        break;
            //    case "weight":
            //        orderBy = "weight" + sortDir;
            //        break;
            //    case "timeStamp":
            //        orderBy = "time_stamp" + sortDir;
            //        break;
            //    case "status":
            //        orderBy = String.Format("is_online {0}, status {1}, last_heartb_time {0}", sortDir, (e.Dir == Ext.Net.SortDirection.DESC ? "ASC" : "DESC"));
            //        break;
            //}

            int totalCount = 0;
            List<Hospitalization> list = null;

            list = hospitalizationService.GetAllByPatientPaged(patientId, orderBy, e.Start / e.Limit, e.Limit, out totalCount);//hospitalizationService.GetAllByPatient(patientId, orderBy);

            if (list != null)
            {
                this.StoreHospital.DataSource = Array.ConvertAll<Hospitalization, object[]>(list.ToArray(),
                    delegate(Hospitalization item)
                    {
                        return new object[]
                        {
                            item.Id,
                            item.Date.ToString("yyyy-MM-dd"),
                            item.ChemistryId.HasValue?"有":"无",
                            item.OpticsId.HasValue?"有":"无",
                            item.SurgeryId.HasValue?"有":"无"
                        };
                    });
                this.StoreHospital.DataBind();
            }
            else
            {
                this.StoreHospital.RemoveAll();
            }

            (this.StorePatientInfo.Proxy[0] as PageProxy).Total = totalCount; 
        }

        #endregion

        #region 高级查询页面

        [DirectMethod]
        public void AdvanceQueryPatient()
        {
            ClearQueryWindowQuery();
            this.WindowQuery.Show();

            RefreshQuery1Item(null, new StoreRefreshDataEventArgs());
            RefreshQuery2Item(null, new StoreRefreshDataEventArgs());
            RefreshQuery3Item(null, new StoreRefreshDataEventArgs());
        }

        private void ClearQueryWindowQuery()
        {
            this.ComboBoxQuery1Chemistry.Reset();
            this.ComboBoxQuery1Chemistry.SetValueAndFireSelect(this.ComboBoxQuery1Chemistry.Items[0].Value);
            this.ComboBoxQuery1Item.Reset();
            this.ComboBoxQuery1Item.Disabled = false;
            this.ComboBoxQuery1Operation.Reset();
            this.ComboBoxQuery1Operation.Disabled = false;
            this.TextFieldQuery1Value.Reset();
            this.ComboBoxQuery1Flag.Reset();

            this.ComboBoxQuery2Chemistry.Reset();
            this.ComboBoxQuery2Chemistry.SetValueAndFireSelect(this.ComboBoxQuery2Chemistry.Items[0].Value);
            this.ComboBoxQuery2Item.Reset();
            this.ComboBoxQuery2Item.Disabled = false;
            this.ComboBoxQuery2Operation.Reset();
            this.ComboBoxQuery2Operation.Disabled = false;
            this.TextFieldQuery2Value.Reset();
            this.ComboBoxQuery2Flag.Reset();
            this.CompositeFieldQuery2.Hidden = true;

            this.ComboBoxQuery3Chemistry.Reset();
            this.ComboBoxQuery3Chemistry.SetValueAndFireSelect(this.ComboBoxQuery3Chemistry.Items[0].Value);
            this.ComboBoxQuery3Item.Reset();
            this.ComboBoxQuery3Item.Disabled = false;
            this.ComboBoxQuery3Operation.Reset();
            this.ComboBoxQuery3Operation.Disabled = false;
            this.TextFieldQuery3Value.Reset();
            this.ComboBoxQuery3Flag.Reset();
            this.CompositeFieldQuery3.Hidden = true;
        }

        protected void RefreshQuery1Item(object sender, StoreRefreshDataEventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(HttpContext.Current.Server.MapPath("./config/Chemistry.xml"));
            List<object> data = new List<object>();

            string selectNodes = "chemistry/type[@text='"+ this.ComboBoxQuery1Chemistry.SelectedItem.Text +"']/item";
            foreach (XmlNode moduleNode in xmlDoc.SelectNodes(selectNodes))
            {
                string text = moduleNode.Attributes["name"].Value;
                string value = moduleNode.Attributes["value"].Value;

                data.Add(new { text = text, value = value });
            }

            this.StoreQuery1Item.DataSource = data;
            this.StoreQuery1Item.DataBind();
            //this.ComboBoxQuery1Item.ClearInvalid();
            //this.ComboBoxQuery1Item.SetValue(this.ComboBoxQuery1Item.Items[0].Text);
        }

        protected void RefreshQuery2Item(object sender, StoreRefreshDataEventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(HttpContext.Current.Server.MapPath("./config/Chemistry.xml"));
            List<object> data = new List<object>();

            string selectNodes = "chemistry/type[@text='"+ this.ComboBoxQuery2Chemistry.SelectedItem.Text +"']/item";
            foreach (XmlNode moduleNode in xmlDoc.SelectNodes(selectNodes))
            {
                string text = moduleNode.Attributes["name"].Value;
                string value = moduleNode.Attributes["value"].Value;

                data.Add(new { text = text, value = value });
            }

            this.StoreQuery2Item.DataSource = data;
            this.StoreQuery2Item.DataBind();
        }

        protected void RefreshQuery3Item(object sender, StoreRefreshDataEventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(HttpContext.Current.Server.MapPath("./config/Chemistry.xml"));
            List<object> data = new List<object>();

            string selectNodes = "chemistry/type[@text='"+ this.ComboBoxQuery3Chemistry.SelectedItem.Text +"']/item";
            foreach (XmlNode moduleNode in xmlDoc.SelectNodes(selectNodes))
            {
                string text = moduleNode.Attributes["name"].Value;
                string value = moduleNode.Attributes["value"].Value;

                data.Add(new { text = text, value = value });
            }

            this.StoreQuery3Item.DataSource = data;
            this.StoreQuery3Item.DataBind();
        }

        [DirectMethod]
        public bool ComboBoxQuery1ChemistrySelect(int index)
        {
            String query1ChemistrySelectText = this.ComboBoxQuery1Chemistry.Items[index].Text;
            if ((query1ChemistrySelectText == "中段尿培养及药敏") || (query1ChemistrySelectText == "结石细菌培养及药敏") || (query1ChemistrySelectText == "结石成分分析"))
            //if((index==7)||(index==8)||(index==9))
            {
                this.ComboBoxQuery1Item.Text = "";
                this.ComboBoxQuery1Item.Disabled = true;
                //this.ComboBoxQuery1Operation.SetValueAndFireSelect(this.ComboBoxQuery1Operation.Items[3].Value);
                this.ComboBoxQuery1Operation.Text = "包含";
                this.ComboBoxQuery1Operation.Disabled = true;
                return false;
            }
            else
            {
                this.ComboBoxQuery1Item.Disabled = false;
                RefreshQuery1Item(null, new StoreRefreshDataEventArgs());
                this.ComboBoxQuery1Operation.Disabled = false;
                this.ComboBoxQuery1Operation.SetValueAndFireSelect(this.ComboBoxQuery1Operation.Items[0].Value);
                return true;
            }
        }

        [DirectMethod]
        public bool ComboBoxQuery1ItemSelect(int index,string setValue)
        {
            if (!string.IsNullOrEmpty(setValue)) this.ComboBoxQuery1Item.Value = setValue;

            String query1ItemSelectText = this.ComboBoxQuery1Item.SelectedItem.Text;
            if ((query1ItemSelectText == "精子活动力") || (query1ItemSelectText == "精子形态") || (query1ItemSelectText == "卵磷脂小体") || (query1ItemSelectText == "红细胞") || (query1ItemSelectText == "白细胞"))
            {
                this.ComboBoxQuery1Operation.Text = "包含";
                this.ComboBoxQuery1Operation.Disabled = true;
                return false;
            }
            else
            {
                this.ComboBoxQuery1Operation.Disabled = false;
                this.ComboBoxQuery1Operation.SetValueAndFireSelect(this.ComboBoxQuery1Operation.Items[0].Value);
                return true;
            }
        }

        [DirectMethod]
        public bool ComboBoxQuery2ChemistrySelect(int index)
        {
            String query2ChemistrySelectText = this.ComboBoxQuery2Chemistry.Items[index].Text;
            if ((query2ChemistrySelectText == "中段尿培养及药敏") || (query2ChemistrySelectText == "结石细菌培养及药敏") || (query2ChemistrySelectText == "结石成分分析"))
            //if((index==7)||(index==8)||(index==9))
            {
                this.ComboBoxQuery2Item.Text = "";
                this.ComboBoxQuery2Item.Disabled = true;
                //this.ComboBoxQuery2Operation.SetValueAndFireSelect(this.ComboBoxQuery2Operation.Items[3].Value);
                this.ComboBoxQuery2Operation.Text = "包含";
                this.ComboBoxQuery2Operation.Disabled = true;
                return false;
            }
            else
            {
                this.ComboBoxQuery2Item.Disabled = false;
                RefreshQuery2Item(null, new StoreRefreshDataEventArgs());
                this.ComboBoxQuery2Operation.Disabled = false;
                this.ComboBoxQuery2Operation.Reset();
                return true;
            }
        }

        [DirectMethod]
        public bool ComboBoxQuery2ItemSelect(int index, string setValue)
        {
            if (!string.IsNullOrEmpty(setValue)) this.ComboBoxQuery2Item.Value = setValue;

            String query2ItemSelectText = this.ComboBoxQuery2Item.SelectedItem.Text;
            if ((query2ItemSelectText == "精子活动力") || (query2ItemSelectText == "精子形态") || (query2ItemSelectText == "卵磷脂小体") || (query2ItemSelectText == "红细胞") || (query2ItemSelectText == "白细胞"))
            {
                this.ComboBoxQuery2Operation.Text = "包含";
                this.ComboBoxQuery2Operation.Disabled = true;
                return false;
            }
            else
            {
                this.ComboBoxQuery2Operation.Disabled = false;
                this.ComboBoxQuery2Operation.SetValueAndFireSelect(this.ComboBoxQuery2Operation.Items[0].Value);
                return true;
            }
        }

        [DirectMethod]
        public bool ComboBoxQuery3ChemistrySelect(int index)
        {
            String query3ChemistrySelectText = this.ComboBoxQuery3Chemistry.Items[index].Text;
            if ((query3ChemistrySelectText == "中段尿培养及药敏") || (query3ChemistrySelectText == "结石细菌培养及药敏") || (query3ChemistrySelectText == "结石成分分析"))
            //if((index==7)||(index==8)||(index==9))
            {
                this.ComboBoxQuery3Item.Text = "";
                this.ComboBoxQuery3Item.Disabled = true;
                //this.ComboBoxQuery3Operation.SetValueAndFireSelect(this.ComboBoxQuery2Operation.Items[3].Value);
                this.ComboBoxQuery3Operation.Text = "包含";
                this.ComboBoxQuery3Operation.Disabled = true;
                return false;
            }
            else
            {
                this.ComboBoxQuery3Item.Disabled = false;
                RefreshQuery3Item(null, new StoreRefreshDataEventArgs());
                this.ComboBoxQuery3Operation.Disabled = false;
                this.ComboBoxQuery3Operation.Reset();
                return true;
            }
        }

        [DirectMethod]
        public bool ComboBoxQuery3ItemSelect(int index, string setValue)
        {
            if (!string.IsNullOrEmpty(setValue)) this.ComboBoxQuery3Item.Value = setValue;

            String query3ItemSelectText = this.ComboBoxQuery3Item.SelectedItem.Text;
            if ((query3ItemSelectText == "精子活动力") || (query3ItemSelectText == "精子形态") || (query3ItemSelectText == "卵磷脂小体") || (query3ItemSelectText == "红细胞") || (query3ItemSelectText == "白细胞"))
            {
                this.ComboBoxQuery3Operation.Text = "包含";
                this.ComboBoxQuery3Operation.Disabled = true;
                return false;
            }
            else
            {
                this.ComboBoxQuery3Operation.Disabled = false;
                this.ComboBoxQuery3Operation.SetValueAndFireSelect(this.ComboBoxQuery3Operation.Items[0].Value);
                return true;
            }
        }

        [DirectMethod]
        public bool QueryPatients()
        {
            try
            {
                StringBuilder sql = new StringBuilder();
                sql.Append(" where id IN (");

                string table1 = "", table2 = "", table3 = "";
                string tableId1 = "", tableId2 = "", tableId3 = "";
                string field1 = "", field2 = "", field3 = "";
                string oper1 = "", oper2 = "", oper3 = "";
                string value1 = "", value2 = "", value3 = "";
                string sql1 = "", sql2 = "", sql3 = "";
                string flag1 = "", flag2 = "";
                string tableChemistry = "tb_chemistry";
                bool queryChemistry = false, queryOptics = false;

                if (String.IsNullOrEmpty(this.TextFieldQuery1Value.Text))
                {
                    X.Msg.Alert("错误", "请先输入查询运算值！").Show();
                    return false;
                }

                String query1ChemistrySelectText = this.ComboBoxQuery1Chemistry.SelectedItem.Text;
                if ((query1ChemistrySelectText == "中段尿培养及药敏") || (query1ChemistrySelectText == "结石细菌培养及药敏") || (query1ChemistrySelectText == "结石成分分析"))
                {
                    //table1 = tableChemistry;
                    field1 = this.ComboBoxQuery1Chemistry.SelectedItem.Value;
                    oper1 = " LIKE "; //this.ComboBoxQuery1Operation.SelectedItem.Value;
                    value1 = "'%" + this.TextFieldQuery1Value.Text + "%'";

                    sql1 = tableChemistry + "." + field1 + oper1 + value1;
                    queryChemistry = true;
                }
                else if (query1ChemistrySelectText == "光学诊断")
                {
                    table1 = this.ComboBoxQuery1Chemistry.SelectedItem.Value;
                    //tableId1 = table1.Substring(3) + "_id";
                    field1 = this.ComboBoxQuery1Item.SelectedItem.Value;
                    oper1 = this.ComboBoxQuery1Operation.SelectedItem.Value;
                    value1 = this.TextFieldQuery1Value.Text;

                    sql1 = table1 + "." + field1 + oper1 + value1;
                    //table1 = ", " + table1;
                    queryOptics = true;
                }
                else
                {
                    table1 = this.ComboBoxQuery1Chemistry.SelectedItem.Value;
                    tableId1 = table1.Substring(3) + "_id";
                    field1 = this.ComboBoxQuery1Item.SelectedItem.Value;

                    if ((table1 == "tb_chemistry_sperm") && ((field1 == "active_power") || (field1 == "status")))
                    {
                        oper1 = " LIKE ";
                        value1 = "'%" + this.TextFieldQuery1Value.Text + "%'";
                    }
                    else if ((table1 == "tb_chemistry_pg") && ((field1 == "lecithin") || (field1 == "rbc") || (field1 == "wbc")))
                    {
                        oper1 = " LIKE ";
                        value1 = "'%" + this.TextFieldQuery1Value.Text + "%'";
                    }
                    else
                    {
                        oper1 = this.ComboBoxQuery1Operation.SelectedItem.Value;
                        value1 = this.TextFieldQuery1Value.Text;
                    }

                    sql1 = tableChemistry + "." + tableId1 + " = " + table1 + ".id AND " + table1 + "." + field1 + oper1 + value1;
                    //table1 = ", " + table1;
                    queryChemistry = true;
                }

                if (this.ComboBoxQuery1Flag.SelectedItem.Value != "0")         //二条查询
                {
                    flag1 = this.ComboBoxQuery1Flag.SelectedItem.Value;


                    if (String.IsNullOrEmpty(this.TextFieldQuery2Value.Text))
                    {
                        X.Msg.Alert("错误", "请先输入查询运算值！").Show();
                        return false;
                    }

                    String query2ChemistrySelectText = this.ComboBoxQuery2Chemistry.SelectedItem.Text;
                    if ((query2ChemistrySelectText == "中段尿培养及药敏") || (query2ChemistrySelectText == "结石细菌培养及药敏") || (query2ChemistrySelectText == "结石成分分析"))
                    {
                        //table2 = tableChemistry;
                        field2 = this.ComboBoxQuery2Chemistry.SelectedItem.Value;
                        oper2 = " LIKE "; //this.ComboBoxQuery2Operation.SelectedItem.Value;
                        value2 = "'%" + this.TextFieldQuery2Value.Text + "%'";

                        sql2 = tableChemistry + "." + field2 + oper2 + value2;
                        queryChemistry = true;
                    }
                    else if (query2ChemistrySelectText == "光学诊断")
                    {
                        table2 = this.ComboBoxQuery2Chemistry.SelectedItem.Value;
                        //tableId1 = table1.Substring(3) + "_id";
                        field2 = this.ComboBoxQuery2Item.SelectedItem.Value;
                        oper2 = this.ComboBoxQuery2Operation.SelectedItem.Value;
                        value2 = this.TextFieldQuery2Value.Text;

                        sql2 = table2 + "." + field2 + oper2 + value2;
                        //table2 = ", " + table2;
                        queryOptics = true;
                    }
                    else
                    {
                        table2 = this.ComboBoxQuery2Chemistry.SelectedItem.Value;
                        tableId2 = table2.Substring(3) + "_id";
                        field2 = this.ComboBoxQuery2Item.SelectedItem.Value;

                        if ((table2 == "tb_chemistry_sperm") && ((field2 == "active_power") || (field2 == "status")))
                        {
                            oper2 = " LIKE ";
                            value2 = "'%" + this.TextFieldQuery2Value.Text + "%'";
                        }
                        else if ((table2 == "tb_chemistry_pg") && ((field2 == "lecithin") || (field2 == "rbc") || (field2 == "wbc")))
                        {
                            oper2 = " LIKE ";
                            value2 = "'%" + this.TextFieldQuery2Value.Text + "%'";
                        }
                        else
                        {
                            oper2 = this.ComboBoxQuery2Operation.SelectedItem.Value;
                            value2 = this.TextFieldQuery2Value.Text;
                        }

                        sql2 = tableChemistry + "." + tableId2 + " = " + table2 + ".id AND " + table2 + "." + field2 + oper2 + value2;
                        //table2 = ", " + table2;
                        queryChemistry = true;
                    }
                }
                if (this.ComboBoxQuery2Flag.SelectedItem.Value != "0")    //三条查询
                {
                    flag2 = this.ComboBoxQuery2Flag.SelectedItem.Value;


                    if (String.IsNullOrEmpty(this.TextFieldQuery3Value.Text))
                    {
                        X.Msg.Alert("错误", "请先输入查询运算值！").Show();
                        return false;
                    }
                    
                    String query3ChemistrySelectText = this.ComboBoxQuery3Chemistry.SelectedItem.Text;
                    if ((query3ChemistrySelectText == "中段尿培养及药敏") || (query3ChemistrySelectText == "结石细菌培养及药敏") || (query3ChemistrySelectText == "结石成分分析"))
                    {
                        //table3 = tableChemistry;
                        field3 = this.ComboBoxQuery3Chemistry.SelectedItem.Value;
                        oper3 = " LIKE "; //this.ComboBoxQuery3Operation.SelectedItem.Value;
                        value3 = "'%" + this.TextFieldQuery3Value.Text + "%'";

                        sql3 = tableChemistry + "." + field3 + oper3 + value3;
                        queryChemistry = true;
                    }
                    else if (query3ChemistrySelectText == "光学诊断")
                    {
                        table3 = this.ComboBoxQuery3Chemistry.SelectedItem.Value;
                        //tableId1 = table1.Substring(3) + "_id";
                        field3 = this.ComboBoxQuery3Item.SelectedItem.Value;
                        oper3 = this.ComboBoxQuery3Operation.SelectedItem.Value;
                        value3 = this.TextFieldQuery3Value.Text;

                        sql3 = table3 + "." + field3 + oper3 + value3;
                        //table3 = ", " + table3;
                        queryOptics = true;
                    }
                    else
                    {
                        table3 = this.ComboBoxQuery3Chemistry.SelectedItem.Value;
                        tableId3 = table3.Substring(3) + "_id";
                        field3 = this.ComboBoxQuery3Item.SelectedItem.Value;

                        if ((table3 == "tb_chemistry_sperm") && ((field3 == "active_power") || (field3 == "status")))
                        {
                            oper3 = " LIKE ";
                            value3 = "'%" + this.TextFieldQuery3Value.Text + "%'";
                        }
                        else if ((table3 == "tb_chemistry_pg") && ((field3 == "lecithin") || (field3 == "rbc") || (field3 == "wbc")))
                        {
                            oper3 = " LIKE ";
                            value3 = "'%" + this.TextFieldQuery3Value.Text + "%'";
                        }
                        else
                        {
                            oper3 = this.ComboBoxQuery3Operation.SelectedItem.Value;
                            value3 = this.TextFieldQuery3Value.Text;
                        }

                        sql3 = tableChemistry + "." + tableId3 + " = " + table3 + ".id AND " + table3 + "." + field3 + oper3 + value3;
                        //table3 = ", " + table3;
                        queryChemistry = true;
                    }
                }

                string tables = "";
                if(!String.IsNullOrEmpty(table1)) tables = ", " + table1;
                if ((!String.IsNullOrEmpty(table2))&&(table2 != table1)) tables = tables + ", " + table2;
                if ((!String.IsNullOrEmpty(table3))&&(table3 != table1) && (table3 != table2)) tables = tables + ", " + table3;

                //sql.Append("select patient_id from tb_hospitalization,tb_chemistry").Append(table1).Append(table2).Append(table3).Append(" where ");
                sql.Append("select patient_id from tb_hospitalization,tb_chemistry").Append(tables).Append(" where ");
                if (queryChemistry) sql.Append("tb_hospitalization.chemistry_id = tb_chemistry.id and ");
                if(queryOptics)sql.Append("tb_hospitalization.optics_id = tb_optics.id and ");

                sql.Append("(");
                sql.Append(sql1).Append(flag1).Append(sql2).Append(flag2).Append(sql3);
                sql.Append("))");

                this.hiddenQuerySql.Text = sql.ToString();
                this.WindowQuery.Hide();
                
                return true;
            }
            catch
            {
                return false;
            }
        }

#endregion



        public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
        {
            try
            {
                FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader br = new BinaryReader(myFile);
                try
                {
                    _Response.AddHeader("Accept-Ranges", "bytes");
                    _Response.Buffer = false;
                    long fileLength = myFile.Length;
                    long startBytes = 0;

                    double pack = 10240; //10K bytes
                    //int sleep = 200;   //每秒5次   即5*10K bytes每秒
                    int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
                    if (_Request.Headers["Range"] != null)
                    {
                        _Response.StatusCode = 206;
                        string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
                        startBytes = Convert.ToInt64(range[1]);
                    }
                    _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
                    if (startBytes != 0)
                    {
                        //Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
                    }
                    _Response.AddHeader("Connection", "Keep-Alive");
                    _Response.ContentType = "application/octet-stream";
                    _Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));

                    br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
                    int maxCount = (int)Math.Floor((fileLength - startBytes) / pack) + 1;

                    for (int i = 0; i < maxCount; i++)
                    {
                        if (_Response.IsClientConnected)
                        {
                            _Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));
                            Thread.Sleep(sleep);
                        }
                        else
                        {
                            i = maxCount;
                        }
                    }
                }
                catch
                {
                    return false;
                }
                finally
                {
                    br.Close();

                    myFile.Close();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}
